javascript 未选中时需要设置无线电组的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13119785/
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-10-26 17:51:19  来源:igfitidea点击:

Need to set default value of radio group when not checked

javascripthtmlformspost

提问by Ashwin

In my html form, I have a group of radio buttons

在我的 html 表单中,我有一组单选按钮

<form action="receive.php" method="post">
    <input type="radio" name="rad" value="one" /> One <br />
    <input type="radio" name="rad" value="two" /> Two <br />
    <input type="radio" name="rad" value="three" /> Three <br />
    <input type="submit" value="submit" />
</form>

I have a scenario where no radio button is checked. In such case, my server-side php receives input value as false. What I want to do is change false to nullor undefined. Is that possible? Any help is greatly appreciated.

我有一个没有选中单选按钮的场景。在这种情况下,我的服务器端 php 接收输入值作为false. 我想要做的是将 false 更改为nullor undefined。那可能吗?任何帮助是极大的赞赏。

回答by

You can do something like this:

你可以这样做:

<form action="receive.php" method="post">
    <input type="radio" name="rad" value="one" /> One <br />
    <input type="radio" name="rad" value="two" /> Two <br />
    <input type="radio" name="rad" value="three" /> Three <br />
    <input type="hidden" name="rad" value="null" />
    <input type="submit" value="submit" />
</form>

Now if you haven't checked any radio button, you will get "null" input value, but remember "null"is not same as NULLin PHP.

现在,如果您还没有选中任何单选按钮,您将获得“空”输入值,但请记住"null"NULLPHP 中的不同。

回答by JAR.JAR.beans

Radio buttons, by definition, do not have a null value.

根据定义,单选按钮没有空值。

You could however add a radio button such as this:

但是,您可以添加一个单选按钮,例如:

<form action="receive.php" method="post">
    <input type="radio" name="rad" value="one" /> One <br />
    <input type="radio" name="rad" value="two" /> Two <br />
    <input type="radio" name="rad" value="three" /> Three <br />
    <input type="radio" name="rad" value="null" /> null <br />
    <input type="submit" value="submit" />
</form>

And use it as the null that you need.

并将其用作您需要的空值。

回答by Jonas Krispin

That might be done with an if/else statement within the .php:

这可以通过 .php 中的 if/else 语句完成:

$radioInput = false;
if (!isset($_POST['rad'])){
    $radioInput = NULL;
}
else {
    $radioInput = $_POST['rad'];
}

http://php.net/manual/en/function.isset.php

http://php.net/manual/en/function.isset.php

回答by qwerty

Use javascript to return nullin case if no radio button is selected,on "submit" click :

如果没有选择单选按钮,请使用javascript返回null,在“提交”点击

var radioChecked=0;
var radios=document.getElementsByName("rad");
for(var i=0;i<radios.length;i++)
{ if(radios[i].checked){
   radioChecked=radioChecked+1;
  }  }
if(radioChecked==0)
return null;

Hope it solves your problem.

希望它能解决你的问题。