简体   繁体   English

删除复选框的“选中”属性

[英]Remove attribute "checked" of checkbox

I need remove the attribute "checked" of one checkbox when errors occur.发生错误时,我需要删除一个复选框的“选中”属性。

The.removeAttr function not work. The.removeAttr function 不起作用。 Any idea?任何的想法? :/ :/

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});
    }
});
*/

尝试...

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

Both of these should work: 这两个都应该工作:

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

AND/OR AND / OR

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

... you can try both together. ......你可以一起试试。

Try this to check 试试看这个

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

and uncheck 并取消选中

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

尝试:

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

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});
    }
});

In case someone's looking for an answer using pure JavaScript:如果有人使用纯 JavaScript 寻找答案:

const audioLabel = document.getElementById('captureAudio');

audioLabel.removeAttribute('checked');

try something like this FIDDLE 试试这样的FIDDLE

    try
      {
        navigator.device.capture.captureImage(function(mediaFiles) {
        console.log("works");
         });
      }

    catch(err)
      {
        alert('hi');
        $("#captureImage").prop('checked', false);

      }

using .removeAttr() on a boolean attribute such as checked , selected , or readonly would also set the corresponding named property to false . 在诸如checkedselectedreadonly类的布尔属性上使用.removeAttr()也会将相应的命名属性设置为false

Hence removed this checked attribute 因此删除了此checked属性

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

I use prop attribute for unchecked the checkbox when errors occur. 当错误发生时,我使用prop属性unchecked checkbox You don't need to use remove property for unchecked your checkbox. 您无需使用remove属性取消选中您的复选框。

$('input#IDName').prop('checked', false);

It is working fine for me. 它对我来说很好。 Hope it will work for you also. 希望它也适合你。

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);
        }, {});
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM