Javascript 删除复选框的“已检查”属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13557623/
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
Remove attribute "checked" of checkbox
提问by FabianoLothor
I need remove the attribute "checked" of one checkbox when errors occur.
发生错误时,我需要删除一个复选框的“已检查”属性。
The .removeAttr function not work. Any idea? :/
.removeAttr 函数不起作用。任何的想法?:/
HTML
HTML
<div data-role="controlgroup" data-type="horizontal" data-mini="true" style="margin-left: auto; margin-right: auto; width: 100%; text-align: center;">
<input type="checkbox" id="captureImage" name="add_image" class="custom" />
<label for="captureImage" data-icon="checkbox">Image</label>
<input type="checkbox" id="captureAudio" name="add_audio" class="custom" />
<label for="captureAudio" data-icon="checkbox">Audio</label>
<input type="checkbox" id="captureVideo" name="add_video" class="custom" />
<label for="captureVideo" data-icon="checkbox">Video</label>
</div>
Javascript
Javascript
$("#captureImage").live("change", function() {
// $("#captureImage").prop('checked', false); // Here Work
if($("#captureImage:checked").val() !== undefined) {
navigator.device.capture.captureImage(function(mediaFiles) {
console.log("works");
}, function(exception) {
$("#captureImage").prop('checked', false); // Not Works Here
_callback.error(exception);
}, {limit: 1});
}
});
/*
$("#captureAudio").live("change", function() {
if($("#captureAudio:checked").val() !== undefined) {
navigator.device.capture.captureAudio(function(mediaFiles) {
console.log("audio");
}, function() {
$("#captureAudio").removeAttr('checked');
_callback.error;
}, {limit: 1});
}
});
$("#captureVideo").live("change", function() {
if($("#captureVideo:checked").val() !== undefined) {
navigator.device.capture.captureVideo(function(mediaFiles) {
console.log("video");
}, function(exception) {
$("#captureVideo").prop('checked', false);
_callback.error(exception);
}, {limit: 1});
}
});
*/
采纳答案by FabianoLothor
Sorry, I solved my problem with the code above:
对不起,我用上面的代码解决了我的问题:
$("#captureImage").live("change", function() {
if($("#captureImage:checked").val() !== undefined) {
navigator.device.capture.captureImage(function(mediaFiles) {
console.log("works");
}, function(exception) {
$("#captureImage").removeAttr('checked').checkboxradio('refresh');
_callback.error(exception);
}, {});
}
});
回答by alex
Try...
尝试...
$("#captureAudio").prop('checked', false);
回答by itsazzad
Both of these should work:
这两个都应该有效:
$("#captureImage").prop('checked', false);
AND/OR
和/或
$("#captureImage").removeAttr('checked');
... you can try both together.
……你可以一起试试。
回答by A. Magalh?es
Try this to check
试试这个检查
$('#captureImage').attr("checked",true).checkboxradio("refresh");
and uncheck
并取消选中
$('#captureImage').attr("checked",false).checkboxradio("refresh");
回答by macool
Try:
尝试:
$("#captureAudio")[0].checked = false;
回答by Sameena cat
using .removeAttr()on a boolean attribute such as checked, selected, or readonlywould also set the corresponding named property to false.
.removeAttr()在布尔属性上使用,例如checked, selected, orreadonly也会将相应的命名属性设置为false。
Hence removed this checked attribute
因此删除了这个选中的属性
$("#IdName option:checked").removeAttr("checked");
回答by Love Pandey
I use prop attribute for uncheckedthe checkboxwhen errors occur.
You don't need to use remove property for unchecked your checkbox.
我使用的道具属性为unchecked在checkbox发生错误时。您不需要使用 remove 属性来取消选中您的复选框。
$('input#IDName').prop('checked', false);
It is working fine for me. Hope it will work for you also.
它对我来说很好。希望它也对你有用。
回答by Logan Wayne
You could try $(this):
你可以试试$(this):
$("#captureAudio").live("change", function() {
if($(this).val() !== undefined) { /* IF THIS VALUE IS NOT UNDEFINED */
navigator.device.capture.captureAudio(function(mediaFiles) {
console.log("audio");
}, function() {
$(this).removeAttr('checked'); /* REMOVE checked ATTRIBUTE; */
/* YOU CAN USE `$(this).prop("checked", false);` ALSO */
_callback.error;
}, {limit: 1});
}
});

