传递单选按钮可重定向到新页面

时间:2014-05-21 06:21:01

标签: javascript php jquery html

我正试图以最好的方式改写这个

我想要它,以便当有人进入某个页面时,他们会获得一个单选按钮的值,该单选按钮是一旦他们点击提交就将其发送到那里的链接。

这里是我试过的代码

 <form name = "quoted" method="get">
     <input id = "poster" type="text" name="poster" required="required" placeholder = "Credited Individual.">     <br>
     <textarea class = "actual_quote" name = "actual_quote" required="required" placeholder = "Write the question here!"></textarea><br><br><br>
     <div class = "checkboxes" required="required">
         <h3 style = "margin-top:-20px;">Please select one catagory that the quote falls into.</h3>
         <label for="x"><input type="radio" name="x" value="stupid.php" id = "x" checked="checked" />    <span>stupid</span></label><br>
         <label for="x"><input type="radio" name="x" value="stupider.php" id = "x" /> <span>stupider</span>    </label><br>
         <label for="x"><input type="radio" name="x" value="stupidest.php" id = "x"/>    <span>stupidest</span></label>
     </div>
     <input id = "submit1" type="submit"><br>
 </form>

这里的代码可以使它工作,但它没有。

$(function(){
    $('form').submit(function(event){
        event.preventDefault();
        window.location = $('input[type=radio]:checked').val();
    });
});
我做错了什么? 我只希望用户能够选择一个无线电选项,一旦选择了该无线电选项,他们按提交将它们重定向到该值所指向的那个特定页面。

请帮忙!欢迎任何建议! -Connor

2 个答案:

答案 0 :(得分:1)

试试这个

<script type="text/javascript">
    function get_action(form) {
        form.action = document.querySelector('input[name = "x"]:checked').value;
    }

</script>


<form name = "quoted" method="get" onsubmit="get_action(this);">
     <input id = "poster" type="text" name="poster" required="required" placeholder = "Credited Individual.">     <br>
     <textarea class = "actual_quote" name = "actual_quote" required="required" placeholder = "Write the question here!"></textarea><br><br><br>
     <div class = "checkboxes" required="required">
         <h3 style = "margin-top:-20px;">Please select one catagory that the quote falls into.</h3>
         <label for="x"><input type="radio" name="x" value="stupid.php" id = "x" checked="checked" />    <span>stupid</span></label><br>
         <label for="x"><input type="radio" name="x" value="stupider.php" id = "x" /> <span>stupider</span>    </label><br>
         <label for="x"><input type="radio" name="x" value="stupidest.php" id = "x"/>    <span>stupidest</span></label>
     </div>
     <input id = "submit1" type="submit"><br>
 </form>

这会奏效。如果有效,请投票。 :)

答案 1 :(得分:0)

您的代码似乎在jsfiddle上正常运行。

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

确保php文件有效且有效。如果页面正在加载但未显示,则可能是您的php文件存在问题。如果单击“提交”时未加载任何页面,请尝试使用其他浏览器,因为此处的代码可以正常工作。