简体   繁体   English

jQuery移动启用按钮(如果已选中复选框)

[英]jQuery mobile enable button if checkbox is checked

I have this checkbox here and I'm trying to do is to enable the button if checkbox is checked but it is not working. 我在这里有此复选框,我想做的是如果复选框已选中但不起作用,则启用该按钮。

HTML: HTML:

<div data-role="fieldcontain">
    <fieldset data-role="controlgroup" style="width:294px">
            <input type="checkbox" name="checkbox-1" id="checkbox-1" class="chk"/>
            <label for="checkbox-1">I agree</label>
    </fieldset>
</div>
<input type="submit" id="submit" data-inline="true" data-theme="a" data-icon="check" value="&nbsp;Submit&nbsp;" disabled/>  

JavaScript: JavaScript的:

<script>
    $('.chk').change(function(){
        if ($(this).is(':checked'))
            {
                $("#submit").removeAttr("disabled");
            }
        else
            {
                $("#submit").attr( "disabled", "disabled" );
            }               
    });
</script>

I can't see what's the problem. 我看不出有什么问题。 And I'm using JQM 1.3.2 我正在使用JQM 1.3.2

$('.chk').change(function(){
    if($(this).is(':checked')) {
        $('#submit').button('enable');  
    }
    else {
        $('#submit').button('disable'); 
    }               
});

You need to use .button() widget to enable or disable a button. 您需要使用.button()小部件来启用禁用按钮。

$(document).on("change", "#checkbox-1", function () {
  if ($(this).prop("checked")) {
    $("#submit").button("enable");
  } else {
    $("#submit").button("disable");
  }
});

Demo 演示版

use button("enable") and button("disable") you are using jquery mobile because 使用button("enable")button("disable")您正在使用jquery mobile,因为

  $('.chk').change(function () {
      if ($(this).is(':checked')) {

          $("#submit").button('enable');
      } else {
          $("#submit").button('disable');
      }
  });

here a fiddle 这里是一个小提琴

When you say: 当你说:

$('object').is(':checked')

It returns a number, the number of the objects that are checked. 它返回一个数字,即已检查对象的数量。

So, for doing things like what you are, you should have an object specific identifier (#submit), and you should revise your if to something like the following. 因此,为了执行类似您自己的操作,您应该具有一个特定于对象的标识符(#submit),并且应该将if修改为以下内容。

$('#submit').is(':checked') > 0

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

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