简体   繁体   English

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

[英]Passing radio buttons to be redirected to a new page

I am trying to reword this the best I can 我正在尽力改写这个

I want it so that when someone goes to a page, they get the value of a radio button that is a link that sends them there once they click submit. 我想要这样,以便当某人转到某个页面时,他们获得单选按钮的值,该单选按钮是一个链接,一旦单击“提交”,就将其发送到该页面。

here's the code i've tried 这是我尝试过的代码

 <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>

and here's the code that is supposed to make it work, but it doesn't. 这是应该使其工作的代码,但是没有。

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

what am I doing wrong? 我究竟做错了什么? I just want the user to be able to select one radio option, and once that radio option is selected and they press submit redirect them to that one certain page that the value is pointing to. 我只希望用户能够选择一个单选选项,并且一旦选择了该单选选项,然后按提交将其重定向到该值所指向的那个特定页面。

Please help! 请帮忙! Any suggestions welcome! 任何建议欢迎! -Connor -Connor

Try this 尝试这个

<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>

This will work. 这将起作用。 Please vote if it worked. 如果可行,请投票。 :) :)

Your code seems to be working fine on jsfiddle . 您的代码在jsfiddle上似乎运行良好。

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

Make sure that the php files are valid and working. 确保php文件有效并且可以正常工作。 If the page is being loaded but not displayed, it might be a problem with your php files. 如果页面正在加载但未显示,则可能是您的php文件有问题。 If no page is being loaded when you click submit, try using a different browser, as the code here works fine. 如果单击提交时没有页面加载,请尝试使用其他浏览器,因为此处的代码可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM