简体   繁体   English

根据复选框已选中的属性,无法在按钮中添加/删除禁用的属性

[英]Unable to add/remove disabled attribute in button based on checkbox checked property

Jquery: jQuery:

<script type="text/javascript">

function  enableDisableButton() {

    var isChecked = ($('input[name=ignore-checkbox]').is(':checked'));

    if (isChecked == true) {
        $("button[name=set-up-account]").removeAttr("disabled");
    }
    else {
        $("button[name=set-up-account]").attr("disabled", "disabled");
    }

}

$('#mobilePage').live('pageinit', function (event) {    //document ready for jquery mobile

    enableDisableButton();

    $('#ignore-checkbox').bind("change", function () {
        enableDisableButton();
    });

});
  </script>

Html: HTML:

 @using (Html.BeginForm("Account", "SetUpAccount", FormMethod.Post, new { @id = "account-set-up", @data_ajax = "false" }))
 {       

     <div class="ui-grid-a field">
        <div class="ui-block-a" style="width:140px;">
         <label for="ignore-checkbox">Ignore</label>
         <input type="checkbox" name="ignore-checkbox" id="ignore-checkbox"/>
        </div>
     </div>

     <div style="width:100%;float:left;">
         <button type="submit" name="set-up-account" id="set-up-account"  data-inline="true"  data-mini="true">Set Up Account</button>
     </div>
  }

I want to enable/disable the Set Up Account button based on check box checked property. 我想启用/禁用基于复选框选中属性的“ Set Up Account按钮。 If checkbox is checked, then enable the button, else disable. 如果选中复选框,则启用按钮,否则禁用。 On page load i am disabling the button 在页面加载中,我禁用了该按钮

The problem i am facing is, 我面临的问题是

On page load, its disabling the button, but its not adding/removing the disabled attribute based on change event of check box, i checked in firebug, its entering the loop properly based on checked and unchecked value. 在页面加载时,其禁用按钮,但不基于复选框的更改事件添加/删除禁用的属性,我在Firebug中进行了检入,它基于选中和未选中的值正确进入了循环。

Note: I am using Jquery mobile 1.2 and jquery 1.7. 注意:我正在使用Jquery mobile 1.2和jQuery 1.7。 Also before posting here i searched in google, there are many postings under enabling and disabling of button based on checkbox checked value. 另外,在这里发布我在谷歌搜索之前,根据复选框的选中值,在启用和禁用按钮下有很多发布。 but none solves my issue. 但没有一个解决我的问题。

What's the problem? 有什么问题? please help me 请帮我

Thanks... 谢谢...

I'm not sure if this will help solve your main problem, but according to http://api.jquery.com/prop/ you should be adding and removing the disabled property using the .prop() function, not the .attr() and .removeAttr() functions. 我不确定这是否有助于解决您的主要问题,但是根据http://api.jquery.com/prop/,您应该使用.prop()函数而不是.attr添加和删除禁用的属性()和.removeAttr()函数。

To add the property: 要添加属性:

.prop("disabled", true);

To remove the property: 删除属性:

.prop("disabled", false);

I stumbled across this question while looking for the above info, so I thought I'd share. 在寻找以上信息时,我偶然发现了这个问题,所以我想分享一下。

Its not enabling the button because of Jquery Mobile. 由于Jquery Mobile,无法启用按钮。 In JQM, we need to refresh the form element, while manipulating it 在JQM中,我们需要在处理表单元素的同时刷新它

The solution is 解决方案是

    function  enableDisableButton() {

    var isChecked = ($('input[name=ignore-checkbox]').is(':checked'));

    if (isChecked) {
        $("button[name='set-up-account']").removeAttr("disabled").button('refresh');
    }
    else {
        $("button[name='set-up-account']").attr("disabled", "disabled").button('refresh');
   }
}

$('#mobilePage').live('pageinit', function (event) {

    enableDisableButton();

    $('#ignore-checkbox').bind("change", function () {
        enableDisableButton();
    });
});

The problem was solved by adding 通过添加解决了问题

.button('refresh');

Like this one 像这个

  $('#setagb').change(function(){
        if ($(this).is(':checked'))
        {
            $("#submit").removeAttr("disabled");
        }
        else
        {
            $("#submit").attr( "disabled", "disabled" );
        }               
  });

Try this 尝试这个

$("input[name=ignore-checkbox]").click(function() {
  $("button[name=set-up-account]").attr("disabled", this.checked);
});

DEMO 演示

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

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