【问题标题】:Remove attribute "checked" of checkbox删除复选框的“选中”属性
【发布时间】:2012-11-13 12:04:45
【问题描述】:

发生错误时,我需要删除一个复选框的“已选中”属性。

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

【问题讨论】:

  • $("#captureImage:checked").val() 将始终为您打开或关闭 val,即使您尚未为其定义值
  • @rajeshkakawat 不,$("#captureImage").val() 与 $("#captureImage:checked").val() 不同
  • 是的,是不同的,但条件是它的作品。

标签: javascript jquery ios cordova jquery-mobile


【解决方案1】:

试试……

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

【讨论】:

  • 不能在回调错误函数中工作,只是在 captureImage 函数中工作。
【解决方案2】:

这两个都应该工作:

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

与/或

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

...你可以同时尝试。

【讨论】:

  • 只有第二个选项对我有用,但我不知道为什么。是不是因为我用attr方法设置了checked属性,比如:radio.attr('checked', 'checked')?
  • @ErikČerpnjak 是的,那么
  • 注意 per .prop('checked',false) or .removeAttr('checked')?.prop( "checked", false ) 总是优于 .removeAttr( "checked" )
【解决方案3】:

试试这个检查

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

并取消选中

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

【讨论】:

    【解决方案4】:

    当发生错误时,我使用 uncheckedcheckbox 的 prop 属性。 您不需要使用删除属性来取消选中您的复选框。

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

    它对我来说很好用。希望它也对你有用。

    【讨论】:

      【解决方案5】:

      试试:

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

      【讨论】:

      • 我猜你做错了条件。试试:if ( $("#captureImage")[0].checked )。避免使用那些 jQuery 伪选择器 (:checked)。
      • @FabianoLothor 也避免引发异常。即使像console.error 这样的东西也可能会使您的代码崩溃。 (发生在我身上)
      • 条件工作正常。问题是我无法在错误函数中运行 JQuery 调用。
      • @FabianoLothor 然后缓存变量。 captureImage = $("#captureImage")(在调用导航器后执行此操作)。然后,在回调函数中,只需:captureImage[0].checked = false
      【解决方案6】:

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

      因此删除了这个选中的属性

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

      【讨论】:

        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

          试试这样的FIDDLE

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

          【讨论】:

          • 我在这里尝试,但是,不准进入。 ://
          【解决方案9】:

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

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

          【讨论】:

            猜你喜欢
            • 2017-08-19
            • 2020-07-28
            • 1970-01-01
            • 1970-01-01
            • 2015-10-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-04
            相关资源
            最近更新 更多