在您的点击处理程序之前添加.unbind('click') 怎么样:
$('#jQueryButton').find('li').button({
disabled: true
}).unbind('click').click(function() {
alert('Hello ' + $(this).closest('fieldset').children('input:first').val() + ' !');
});
演示:http://jsfiddle.net/jtbowden/yPHKp/
当然,那么你必须将智能添加到点击处理程序中以在输入为空时不执行:
$('#jQueryButton').find('li').button({
disabled: true
}).unbind('click').click(function() {
if($('#jQueryButton').children('input').val()) {
alert('Hello ' + $(this).closest('fieldset').children('input:first').val() + ' !');
}
});
演示:http://jsfiddle.net/jtbowden/yPHKp/1/
或者,您可以添加一个keyup 处理程序。然后在单击按钮之前进行更改:
$('#jQueryButton').children('input').bind('change keyup', function() {
var self = $(this);
self.closest('fieldset').find('.ui-button').button(self.val() ? 'enable' : 'disable');
});
演示:http://jsfiddle.net/jtbowden/GcnYz/1/
更好的解决方案可能是unbind click.button 处理程序,然后通过在输入字段中添加.blur() 事件重新绑定它:
$('#jQueryButton').find('li').button({
disabled: true
}).unbind('click.button').bind('click.button', function(event) {
$('#jQueryButton').children('input').blur();
if ($(this).button("option", "disabled")) {
event.preventDefault();
event.stopImmediatePropagation();
}
}).click(function() {
alert('Hello ' + $(this).closest('fieldset').children('input:first').val() + ' !');
});
这将保留您的所有验证,并强制change 事件在按钮单击之前发生。您可以将最终的 .click() 处理程序集成到 click.button 事件中,方法是将胆量放在 else 块中。
演示:http://jsfiddle.net/jtbowden/GcnYz/2/