sys*_*ult 463 javascript jquery radio-button dynamic-forms
在使用jQuery提交AJAX表单后,我想要取消选中一组单选按钮.我有以下功能:
function clearForm(){
$('#frm input[type="text"]').each(function(){
$(this).val("");
});
$('#frm input[type="radio":checked]').each(function(){
$(this).checked = false;
});
}
Run Code Online (Sandbox Code Playgroud)
在此功能的帮助下,我可以清除文本框中的值,但我无法清除单选按钮的值.
顺便说一句,我也试过$(this).val("");但是没用.
Dav*_*und 714
要么(普通的js)
this.checked = false;
Run Code Online (Sandbox Code Playgroud)
或者(jQuery)
$(this).prop('checked', false);
// Note that the pre-jQuery 1.6 idiom was
// $(this).attr('checked', false);
Run Code Online (Sandbox Code Playgroud)
有关attr()和prop()之间差异的解释以及为什么prop()现在更可取,请参阅jQuery prop()帮助页面.
prop()于2011年5月与jQuery 1.6一起推出.
Jam*_*man 85
你不需要这个each功能
$("input:radio").attr("checked", false);
Run Code Online (Sandbox Code Playgroud)
要么
$("input:radio").removeAttr("checked");
Run Code Online (Sandbox Code Playgroud)
同样也适用于您的文本框:
$('#frm input[type="text"]').val("");
Run Code Online (Sandbox Code Playgroud)
但你可以改善这一点
$('#frm input:text').val("");
Run Code Online (Sandbox Code Playgroud)
cjs*_*hno 29
尝试
$(this).removeAttr('checked')
Run Code Online (Sandbox Code Playgroud)
因为很多浏览器会将'checked = anything'解释为true.这将完全删除checked属性.
希望这可以帮助.
alk*_*333 20
基于Igor代码对Laurynas插件的轻微修改.这可以容纳与目标单选按钮相关联的可能标签:
(function ($) {
$.fn.uncheckableRadio = function () {
return this.each(function () {
var radio = this;
$('label[for="' + radio.id + '"]').add(radio).mousedown(function () {
$(radio).data('wasChecked', radio.checked);
});
$('label[for="' + radio.id + '"]').add(radio).click(function () {
if ($(radio).data('wasChecked'))
radio.checked = false;
});
});
};
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
igo*_*gor 18
谢谢帕特里克,你结束了我的一天!这是你必须使用的mousedown.但是我改进了代码,因此您可以处理一组单选按钮.
//We need to bind click handler as well
//as FF sets button checked after mousedown, but before click
$('input:radio').bind('click mousedown', (function() {
//Capture radio button status within its handler scope,
//so we do not use any global vars and every radio button keeps its own status.
//This required to uncheck them later.
//We need to store status separately as browser updates checked status before click handler called,
//so radio button will always be checked.
var isChecked;
return function(event) {
//console.log(event.type + ": " + this.checked);
if(event.type == 'click') {
//console.log(isChecked);
if(isChecked) {
//Uncheck and update status
isChecked = this.checked = false;
} else {
//Update status
//Browser will check the button by itself
isChecked = true;
//Do something else if radio button selected
/*
if(this.value == 'somevalue') {
doSomething();
} else {
doSomethingElse();
}
*/
}
} else {
//Get the right status before browser sets it
//We need to use onmousedown event here, as it is the only cross-browser compatible event for radio buttons
isChecked = this.checked;
}
}})());
Run Code Online (Sandbox Code Playgroud)
Lau*_*nas 17
将Igor的代码重写为插件.
使用:
$('input[type=radio]').uncheckableRadio();
Run Code Online (Sandbox Code Playgroud)
插入:
(function( $ ){
$.fn.uncheckableRadio = function() {
return this.each(function() {
$(this).mousedown(function() {
$(this).data('wasChecked', this.checked);
});
$(this).click(function() {
if ($(this).data('wasChecked'))
this.checked = false;
});
});
};
})( jQuery );
Run Code Online (Sandbox Code Playgroud)
小智 16
对于无线电和无线电组:
$(document).ready(function() {
$(document).find("input:checked[type='radio']").addClass('bounce');
$("input[type='radio']").click(function() {
$(this).prop('checked', false);
$(this).toggleClass('bounce');
if( $(this).hasClass('bounce') ) {
$(this).prop('checked', true);
$(document).find("input:not(:checked)[type='radio']").removeClass('bounce');
}
});
});
Run Code Online (Sandbox Code Playgroud)
小智 14
试试这个,这样就可以了:
$(document).ready(function() {
$("input[type='radio']").mousedown(function(e) {
if ($(this).attr("checked") == true) {
setTimeout("$('input[id=" + $(this).attr('id') + "]').removeAttr('checked');", 200);}
else {
return true
}
});
});
Run Code Online (Sandbox Code Playgroud)
您还可以仅使用复选框模拟radiobutton行为:
<input type="checkbox" class="fakeRadio" checked />
<input type="checkbox" class="fakeRadio" />
<input type="checkbox" class="fakeRadio" />
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用这个简单的代码为您工作:
$(".fakeRadio").click(function(){
$(".fakeRadio").prop( "checked", false );
$(this).prop( "checked", true );
});
Run Code Online (Sandbox Code Playgroud)
它工作正常,您可以更好地控制每个按钮的行为.
您可以自己尝试:http://jsfiddle.net/almircampos/n1zvrs0c/
这个分支还允许您在评论中取消选择所有内容:http: //jsfiddle.net/5ry8n4f4/
您可以使用此 JQuery 取消选中单选按钮
$('input:radio[name="IntroducerType"]').removeAttr('checked');
$('input:radio[name="IntroducerType"]').prop('checked', false);
Run Code Online (Sandbox Code Playgroud)
只需为jQuery添加以下代码:
jQuery("input:radio").removeAttr("checked");
Run Code Online (Sandbox Code Playgroud)
对于javascript:
$("input:radio").removeAttr("checked");
Run Code Online (Sandbox Code Playgroud)
没有必要放任何foreach循环,.each()fubction或任何东西
小智 5
$('#frm input[type="radio":checked]').each(function(){
$(this).checked = false;
});
Run Code Online (Sandbox Code Playgroud)
这几乎很好,但你错过了 [0]
正确->>$(this)[0].checked = false;
用这个
$("input[name='nameOfYourRadioButton']").attr("checked", false);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
610290 次 |
| 最近记录: |