簡體   English   中英

刪除復選框的“選中”屬性

[英]Remove attribute "checked" of checkbox

發生錯誤時,我需要刪除一個復選框的“選中”屬性。

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

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;

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

如果有人使用純 JavaScript 尋找答案:

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

audioLabel.removeAttribute('checked');

試試這樣的FIDDLE

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

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

      }

在諸如checkedselectedreadonly類的布爾屬性上使用.removeAttr()也會將相應的命名屬性設置為false

因此刪除了此checked屬性

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

當錯誤發生時,我使用prop屬性unchecked checkbox 您無需使用remove屬性取消選中您的復選框。

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

它對我來說很好。 希望它也適合你。

對不起,我用上面的代碼解決了我的問題:

$("#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