打开页面的单选按钮单选、按钮、页面

2023-09-06 19:25:00 作者:老板、来瓶敌敌畏

我有一个带有 2 个单选按钮和一个提交按钮的表单.两个单选按钮都有不同页面的 url,比如一个会打开 google.com,另一个会打开 yahoo.com.已选择单选按钮并提交页面.我希望它打开我的网址.请帮忙.带有 HTML 和 javascript 代码.提前致谢.

i have a form with 2 radio buttons and a submit button. Both radio buttons have url of different pages, like one will open google.com and another will open yahoo.com. have selected a radio button and submit the page. i want it to open my url. Please help. with HTML and javascript code. Thanks in advance.

推荐答案

为自己节省 90k 额外脚本:

Save yourself 90k of extra script:

<script type="text/javascript">

  function doSubmit(form) {
    var urls = form['url'];
    var i = urls && urls.length;
    while (i--) {
      if (urls[i].checked) {
        window.location = urls[i].value;
      }
    }
    return false;
  }

  </script>

  <form action="" onsubmit="return doSubmit(this)">
    <input type="radio" name="url" value="http://www.google.com"> Google
    <input type="radio" name="url" value="http://www.yahoo.com"> Yahoo
    <input type="submit" value="Submit">
  </form>

哦,永远不要给表单控件命名为submit",因为它会掩盖表单的提交方法.如果必须,请将其命名为submitButton"或类似名称.

Oh, and never give a form control a name of "submit" as it will mask the form's submit method. If you must, name it "submitButton" or similar.