Radio button uncheck on second click(第二次单击时取消选中单选按钮)
问题描述
我有很多单选按钮可以从我的数据库中获取一个值,如果它设置为1",我会选中单选按钮.
I have a LOT of radio buttons that grab a value from my database and if it is set to "1", I make the radio button checked.
如果选中了单选按钮,并且用户再次单击它,我仍然希望能够清除此按钮.有人有想法吗?
If a radio button is checked, and a user clicks on it again, I still want to be able to clear this button. Does anyone have an idea?
$radio1 从数据库中获取数据,为 0、1 或 2
$radio1 grabs data from database and will be either 0, 1, or 2
<input value="1" name="radio1" type="radio"<?php if($radio1==1){echo " checked";} ?>>
<input value="2" name="radio2" type="radio"<?php if($radio1==2){echo " checked";} ?>>
Varun Malhotra 的回答稍作修改:我更改了两行对我来说效果更好的代码.但总的来说,Varun 的回答是完美的!
Varun Malhotra's Answer slightly modified: I changed 2 lines of code that worked a little better for me. But overall, Varun's answer was PERFECT!
$('input[type="radio"]').click(function(){
var $radio = $(this);
// if this was previously checked
if ($radio.data('waschecked') == true)
{
$radio.prop('checked', false);
$radio.data('waschecked', false);
}
else
{
$radio.prop('checked', true);
$radio.data('waschecked', true);
}
// remove was checked from other radios
$radio.siblings('input[type="radio"]').data('waschecked', false);
});
推荐答案
我建议添加一个自定义属性来跟踪每个收音机的先前状态,如下所示:
I'll suggest to add a custom attribute to keep track of each radio's previous state like so:
$(function(){
$('input[name="rad"]').click(function(){
var $radio = $(this);
// if this was previously checked
if ($radio.data('waschecked') == true)
{
$radio.prop('checked', false);
$radio.data('waschecked', false);
}
else
$radio.data('waschecked', true);
// remove was checked from other radios
$radio.siblings('input[name="rad"]').data('waschecked', false);
});
});
您还需要将此属性添加到最初检查的单选标记中
You will also need to add this attribute to the initially checked radio markup
<input type="radio" name="rad" id="Radio0" checked="checked" data-waschecked="true" />
JSFIDDLE 演示
更新:
$(function(){
$('input[name="rad"]').click(function(){
var $radio = $(this);
// if this was previously checked
if ($radio.data('waschecked') == true)
{
$radio.prop('checked', false);
$radio.data('waschecked', false);
}
else
$radio.data('waschecked', true);
// remove was checked from other radios
$radio.siblings('input[type="radio"]').data('waschecked', false);
});
});
但请确保您没有其他单选组可以使用,否则您必须提供一些属性来指定这些按钮,例如我之前关注的 name prop.
But do ensure that you don't have other radio-groups to work with, otherwise you have to provide some attributes to specify these buttons like name prop I focused earlier.
这篇关于第二次单击时取消选中单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:第二次单击时取消选中单选按钮
基础教程推荐
- PHP 类:全局变量作为类中的属性 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
