删除checkbox的“已选中”属性

发生错误时,我需要删除一个checkbox的“checked”属性。

.removeAttr函数不起作用。 任何想法? :/

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

 $("#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}); } }); */ 

尝试…

 $("#captureAudio").prop('checked', false); 

这两个应该工作:

 $("#captureImage").prop('checked', false); 

AND / OR

 $("#captureImage").removeAttr('checked'); 

你可以一起尝试

试试这个来检查

 $('#captureImage').attr("checked",true).checkboxradio("refresh"); 

并取消选中

 $('#captureImage').attr("checked",false).checkboxradio("refresh"); 

尝试:

 $("#captureAudio")[0].checked = false; 

尝试像这样的FIDDLE

  try { navigator.device.capture.captureImage(function(mediaFiles) { console.log("works"); }); } catch(err) { alert('hi'); $("#captureImage").prop('checked', false); } 

你可以试试$(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}); } }); 

在布尔属性(如checkedselectedreadonly .removeAttr()上使用.removeAttr()也会将相应的named属性设置为false

因此删除了这个检查属性

 $("#IdName option:checked").removeAttr("checked"); 

对不起,我用上面的代码解决了我的问题:

 $("#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); }, {}); } });