我正在使用: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);
}
此代码给我错误:无法在初始化之前调用微调器上的方法;尝试调用方法“选项”
最佳答案
jQuery UI 微调器有 enable/disable默认情况下的方法,最好利用这些方法。此外,如果您想默认禁用微调器,只需将其输入设置为 disabled="disabled"
,然后将 this.disabled
提供给微调器构造函数。如果您使用此方法,则每次将 this.disabled
提供给微调器构造函数是安全的,因为 this.disabled
=== true
if disabled="disabled"
和 this.disabled
=== false
如果 disabled
属性不存在。
<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>
关于JQuery UI 微调器禁用复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14274064/