HTML:在单选按钮检查上提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18564205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:55:11  来源:igfitidea点击:

HTML: Submit form on radio button check

html

提问by Juicy

I have a form that displays two radio buttons (using PHP).

我有一个显示两个单选按钮的表单(使用 PHP)。

echo "<form action=\"next.php\" method=\"post\">";
   echo "<input type=\"radio\" name=\"paid\" value=\"0\" checked=\"checked\">No<br>";
   echo "<input type=\"radio\" name=\"paid\" value=\"1\">Yes<br>";
echo "</form>";

One of the radio buttons is always checked by default. If the user checks the other radio button, I would like the form to submit itself (without the user having to click a submit button).

默认情况下始终选中单选按钮之一。如果用户选中另一个单选按钮,我希望表单自行提交(用户无需单击提交按钮)。

回答by Roko C. Buljan

you can append this into your input:

您可以将其附加到您的输入中:

onchange='this.form.submit();'

like:

喜欢:

echo "<form action=\"next.php\" method=\"post\">";
   echo "<input onchange='this.form.submit();' type=\"radio\" name=\"paid\" value=\"0\" checked=\"checked\">No<br>";
   echo "<input onchange='this.form.submit();' type=\"radio\" name=\"paid\" value=\"1\">Yes<br>";
echo "</form>";

DEMO

演示

回答by Charaf JRA

You can do this using jQuery (put the code inside the </body>tag):

您可以使用 jQuery 执行此操作(将代码放在</body>标签中):

<script type='text/javascript'>

 $(document).ready(function() { 
   $('input[name=paid]').change(function(){
        $('form').submit();
   });
  });

</script>

DEMO HERE

演示在这里

To add jQuery to your page, put this line inside the <head>tag:

要将 jQuery 添加到您的页面,请将此行放在<head>标记中:

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'>
</script>

回答by Gary Hayes

Use an onchange listener:

使用 onchange 侦听器:

$("#paid").change(function(){
alert("test");
});

You'll just have to give your radio button an id to go along with the name. The ID can be the same as the name, to keep it simple for you.

你只需要给你的单选按钮一个 id 与名称一起使用。ID 可以与名称相同,以保持简单。