JQuery UI微调器禁用复选框

时间:2013-01-11 08:08:37

标签: jquery jquery-ui jquery-ui-spinner

我正在使用:http://api.jqueryui.com/spinner如何设置它,以便如果输入attr = disabled,它将禁用微调器?

<input type="checkbox" />
<input type="text" class="spinner" />

if($checkbox.attr("checked")){
   $input.removeAttr('disabled').spinner();
} else {
   $input.attr('disabled', true).spinner("option", "disabled", true);
}

这段代码给我错误:在初始化之前无法在微调器上调用方法;试图调用方法'选项'

3 个答案:

答案 0 :(得分:3)

jQuery UI微调器默认具有enable / disable方法,最好利用这些方法。此外,如果要将微调器默认为禁用,只需将其输入设置为disabled="disabled",然后将this.disabled提供给微调器构造函数。如果您使用此方法,则每次向{spid]构造函数提供this.disabled是安全的,因为this.disabled === true如果disabled="disabled"this.disabled ==如果false属性不存在,则为disabled

http://jsfiddle.net/A3SL4/

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"/>
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    </head>
    <body>
        <p>
            <label for="spinner1">Default Enabled Spinner</label>
            <input class="spinner" id="spinner1"/>
            <input checked="checked" onchange="enableSpinner('spinner1', this.checked);" type="checkbox"/>
        </p>
        <p>
            <label for="spinner2">Default Disabled Spinner</label>
            <input class="spinner" disabled="disabled" id="spinner2"/>
            <input onchange="enableSpinner('spinner2', this.checked);" type="checkbox"/>
        </p>
        <script type="text/javascript">
            $(".spinner").each(function() {
                $(this).spinner({
                    disabled: this.disabled
                });
            });

            function enableSpinner(spinnerId, enable) {
                var spinner = $("#" + spinnerId);
                if (enable) {
                    spinner.spinner("enable");
                } else {
                    spinner.spinner("disable");
                }
            }
        </script>
    </body>
</html>

答案 1 :(得分:2)

您正在尝试在未创建微调框小部件时设置该选项。相反,您可以使用构造函数选项disable调用您的微调器:

if($checkbox.attr("checked")){
   $input.removeAttr('disabled').spinner();
} else {
   $input.attr('disabled', true).spinner({
      disabled: true
    });
}

答案 2 :(得分:2)

试试这个:http://jsbin.com/ojiwas/1/edit

$( ".spinner" ).prop('disabled',true);
  $(':checkbox').change(function(){
    if($(':checked').length>0){
      $( ".spinner" ).spinner();
    }else{
      $( ".spinner" ).spinner("destroy").prop('disabled',true);
    }
});
相关问题