Radio Button to open pages

♀尐吖头ヾ 提交于 2019-12-19 05:01:07

问题


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.


回答1:


Do something like this

<?
if ($_POST['submitUrl']){
header('Location:'.$_POST['url']);
}
?>
<form method="post" action="">
<input type="radio" name="url" value="http://www.google.com"/>Link1<br>
<input type="radio" name="url" value="http://www.yahoo.com"/>Link2<br>
<input type="submit" name"submitUrl" value="Submit"/>
</form>



回答2:


HTML

<form name="form" method="post" action="">
    <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" name="submit" value="Submit" />
</form>

JQuery

$(function(){
    $('form').submit(function(event){
        event.preventDefault();
        window.location = $('input[type=radio]:checked').val();
    });
});

Working example: http://jsfiddle.net/AlienWebguy/Rpzvd/3/




回答3:


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>

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.



来源:https://stackoverflow.com/questions/6741340/radio-button-to-open-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!