如果只有一个按钮,如何取消选中单选按钮?
How is a radio button unchecked, if there's only one button?
我在输入表单中有这两个单选按钮:
<input type="radio" name="info_only_on" value="yes"> Info-only
<input type="radio" name="info_only_on" value="off"> (Clear Button)
它创建了两个按钮,因此用户可以检查仅信息,然后在他们犯错时关闭仅信息。我创建了第二个按钮,因为一旦选中单选按钮,再次单击它并不会取消选择它。
我已经切换到 type="checkbox",它允许用户取消选择。
<input type="checkbox" name="info_only_on" value="yes">
查看单选类型按钮的规格,我没有看到用户取消选中它的任何内容。我错过了什么?
我正在使用 html、php 并避免使用 javascript。
用于勾选的php值是:
// When info_only_on is set to clear, it's value should be passed here as "no"
if($_POST["info_only_on"] == "yes")
{ $info_only = "Added Member info online but not paying online. "; }
else
{ $info_only = " "; }
// BUILD UP MESSAGE to email to our membership chair
$MsgToWrite = "\r\n" . $BasicInfo . $PhoneInfo . $EmailInfo;
If ($info_only <> " ")
{ $MsgToWrite = $MsgToWrite . "\r\n" . $info_only; }
遗憾的是,无法使用 HTML 取消选择单个单选按钮。在HTML中,正好需要选择一个单选按钮。如果您想在选择一个单选按钮后取消选择所有单选按钮,则必须为此使用 javascript。
来自维基百科:"It is possible that initially none of the radio buttons in a group are selected. This unselected state cannot be restored by interacting with the radio button widget, though it may be possible through other user interface elements." https://en.wikipedia.org/wiki/Radio_button
我认为没有 javascript 是做不到的,这里有一个简单的例子:
HTML 代码:
<input type="radio" name="name" id="radioBtn" onclick="test(this)" /> Radio
Javascript代码:
var radioState = false;
function test(element){
if(radioState == false) {
check();
radioState = true;
}else{
uncheck();
radioState = false;
}
}
function check() {
document.getElementById("radioBtn").checked = true;
}
function uncheck() {
document.getElementById("radioBtn").checked = false;
}
我在输入表单中有这两个单选按钮:
<input type="radio" name="info_only_on" value="yes"> Info-only
<input type="radio" name="info_only_on" value="off"> (Clear Button)
它创建了两个按钮,因此用户可以检查仅信息,然后在他们犯错时关闭仅信息。我创建了第二个按钮,因为一旦选中单选按钮,再次单击它并不会取消选择它。
我已经切换到 type="checkbox",它允许用户取消选择。
<input type="checkbox" name="info_only_on" value="yes">
查看单选类型按钮的规格,我没有看到用户取消选中它的任何内容。我错过了什么?
我正在使用 html、php 并避免使用 javascript。
用于勾选的php值是:
// When info_only_on is set to clear, it's value should be passed here as "no"
if($_POST["info_only_on"] == "yes")
{ $info_only = "Added Member info online but not paying online. "; }
else
{ $info_only = " "; }
// BUILD UP MESSAGE to email to our membership chair
$MsgToWrite = "\r\n" . $BasicInfo . $PhoneInfo . $EmailInfo;
If ($info_only <> " ")
{ $MsgToWrite = $MsgToWrite . "\r\n" . $info_only; }
遗憾的是,无法使用 HTML 取消选择单个单选按钮。在HTML中,正好需要选择一个单选按钮。如果您想在选择一个单选按钮后取消选择所有单选按钮,则必须为此使用 javascript。
来自维基百科:"It is possible that initially none of the radio buttons in a group are selected. This unselected state cannot be restored by interacting with the radio button widget, though it may be possible through other user interface elements." https://en.wikipedia.org/wiki/Radio_button
我认为没有 javascript 是做不到的,这里有一个简单的例子:
HTML 代码:
<input type="radio" name="name" id="radioBtn" onclick="test(this)" /> Radio
Javascript代码:
var radioState = false;
function test(element){
if(radioState == false) {
check();
radioState = true;
}else{
uncheck();
radioState = false;
}
}
function check() {
document.getElementById("radioBtn").checked = true;
}
function uncheck() {
document.getElementById("radioBtn").checked = false;
}