Die*_*oP. 40 javascript jquery radio
我有这个代码来检查/取消选中一个单选按钮.
我知道这对UI不好,但我需要这个.
$('#radioinstant').click(function() {
var checked = $(this).attr('checked', true);
if(checked){
$(this).attr('checked', false);
}
else{
$(this).attr('checked', true);
}
});
Run Code Online (Sandbox Code Playgroud)
上述功能无效.
如果我点击按钮,则没有任何变化.它仍然是检查.为什么?错误在哪里?我不是jQuery专家.我在jQuery 1.3.2上
要清楚的#radioinstant是单选按钮的ID.
小智 38
我已经扩展了之前的建议.这适用于我,多个无线电通过相同的名称耦合.
$("input[type='radio']").click(function()
{
var previousValue = $(this).attr('previousValue');
var name = $(this).attr('name');
if (previousValue == 'checked')
{
$(this).removeAttr('checked');
$(this).attr('previousValue', false);
}
else
{
$("input[name="+name+"]:radio").attr('previousValue', false);
$(this).attr('previousValue', 'checked');
}
});
Run Code Online (Sandbox Code Playgroud)
Bri*_*y37 23
如果你想要做的就是有一个检查的复选框,不要担心使用JQuery.这是点击复选框的默认功能.但是,如果您想要执行其他操作,可以使用JQuery添加它们.在jQuery 1.9之前,您可以使用use $(this).attr('checked');来获取值,而不是$(this).attr('checked', true);,因为第二个将设置值.
这是一个小提示演示,显示默认复选框功能与您使用JQuery的操作.
注意: 在JQuery 1.6之后,你应该使用$(this).prop;而不是$(this).attr在所有三个地方(感谢@Whatevo指出这一点,并在此处查看更多详细信息).
更新:
对不起,错过了必须是单选按钮的要求.您仍然可能需要考虑复选框,但这里是无线电输入版本的更新代码.它的工作原理是previousValue在复选框上设置属性,因为我认为prop1.3.2 不支持.您也可以在范围变量中执行此操作,因为有些人不喜欢在字段上设置随机属性. 这是新的例子.
更新2:
正如Josh指出的那样,之前的解决方案仅适用于一个单选按钮.这是一个功能,使一组无线电可以通过其名称和小提琴取消选择:
var makeRadiosDeselectableByName = function(name){
$('input[name=' + name + ']').click(function() {
if($(this).attr('previousValue') == 'true'){
$(this).attr('checked', false)
} else {
$('input[name=' + name + ']').attr('previousValue', false);
}
$(this).attr('previousValue', $(this).attr('checked'));
});
};
Run Code Online (Sandbox Code Playgroud)
man*_*nji 13
您没有获取选中的值,而是使用以下方法设置:
var checked = $(this).attr('checked', true);
Run Code Online (Sandbox Code Playgroud)
要正确得到它:
var checked = $(this).attr('checked');
Run Code Online (Sandbox Code Playgroud)
// select radio buttons group (same name)
var radioButtons = $("input[type='radio'][name='rr']");
// save initial ckecked states
var radioStates = {};
$.each(radioButtons, function(index, rd) {
radioStates[rd.value] = $(rd).is(':checked');
});
// handle click event
radioButtons.click(function() {
// check/unchek radio button
var val = $(this).val();
$(this).prop('checked', (radioStates[val] = !radioStates[val]));
// update other buttons checked state
$.each(radioButtons, function(index, rd) {
if(rd.value !== val) {
radioStates[rd.value] = false;
}
});
});
Run Code Online (Sandbox Code Playgroud)
PS: $().attr应该用来代替$().propjquery <1.6
Sla*_*ser 12
最好和最短的解决方案.它适用于任何一组无线电(具有相同的名称).
$(document).ready(function(){
$("input:radio:checked").data("chk",true);
$("input:radio").click(function(){
$("input[name='"+$(this).attr("name")+"']:radio").not(this).removeData("chk");
$(this).data("chk",!$(this).data("chk"));
$(this).prop("checked",$(this).data("chk"));
$(this).button('refresh'); // in case you change the radio elements dynamically
});
});
Run Code Online (Sandbox Code Playgroud)
更多信息,点击这里.
请享用.
| 归档时间: |
|
| 查看次数: |
225202 次 |
| 最近记录: |