单选按钮打开页面 [英] Radio Button to open pages

查看:34
本文介绍了单选按钮打开页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个单选按钮和一个提交按钮的表单.两个单选按钮的网址都不同,例如一个将打开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>

哦,从不给表单控件一个提交"的名称,因为它将掩盖表单的提交方法.如果必须的话,将其命名为"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.

这篇关于单选按钮打开页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆