jQuery 对话框按钮重新绑定
我有一个简单的 jQuery 对话框,里面有一个 html 输入。对话框中有两个按钮“保存”/“取消”。当用户点击“保存”时,我验证他是否在输入框中提供了任何输入。如果他没有,我会在对话框中的新行中报告错误并等待他提供输入。但当他再次点击“保存”时,什么也没有发生。使用后如何重新绑定“保存”按钮?谢谢!
var message = "Please provide Phone number."
var phoneNum = "<input type='text' id='phoneNum' name='phoneNum'/> ";
var dialogHtml = "<div id='confirmPhoneNumber'>" + message + phoneNum + "<div id='dialogError' style='display:none'>Phone number field cannot be empty</div>" + "</div>";
$(this).append(dialogHtml);
$("#confirmPhoneNumber").css("font-size", "70%");
$("#confirmPhoneNumber").dialog({
resizable : false,
modal : true,
title : 'Save Phone',
buttons : {
'Save' : function () {
// Phone number must be provided
if ($("#phoneNum").val().trim().length == 0) {
$("#dialogError").show();
} else {
// Make AJAX call
}
},
'Cancel' : function () {
$(this).dialog('close');
}
}
});
I have a simple jQuery dialog box that has an html input inside it. There are two buttons Save/Cancel on the dialog box. When the user hits Save, I validate if he has provided any input in the input box. If he hasn't, I report the error on a new line in the dialog box and wait for him to provide input. But when he hits Save again, nothing happens. How can I re-bind the Save button once it has been used? Thanks!
var message = "Please provide Phone number."
var phoneNum = "<input type='text' id='phoneNum' name='phoneNum'/> ";
var dialogHtml = "<div id='confirmPhoneNumber'>" + message + phoneNum + "<div id='dialogError' style='display:none'>Phone number field cannot be empty</div>" + "</div>";
$(this).append(dialogHtml);
$("#confirmPhoneNumber").css("font-size", "70%");
$("#confirmPhoneNumber").dialog({
resizable : false,
modal : true,
title : 'Save Phone',
buttons : {
'Save' : function () {
// Phone number must be provided
if ($("#phoneNum").val().trim().length == 0) {
$("#dialogError").show();
} else {
// Make AJAX call
}
},
'Cancel' : function () {
$(this).dialog('close');
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
实际上代码正在运行。这里唯一的事情是你无法意识到它,因为错误配置的测试场景(至少我这么认为,哈哈)
如果你测试这个,你会发现根本没有解除绑定。
编辑:重新配置“如果签入保存”按钮。希望这会起作用。
actually the code is working. only thing here is you can't realize it because of miss-configured test scenario ( at least I think so, LoL )
if you test this, you will see that there is no unbinding at all.
EDİT : re-configured the if check in save button. hope this will work.
你用IE测试吗?您在页面上看到任何错误吗?
如果你看一下(我正在使用 chrome 进行测试) 这里< /strong> 它有效,它允许您按下按钮,它会显示错误,输入一些内容,再次单击它会发出警报。所以按钮仍然有效。
但是,IE 不起作用 - 它显示错误......
.trim()
不是 IE 支持的函数 。因此,您可以使用 jQuery 的 .trim() 代替:尝试再次此处它适用于 IE。
看看在 IE 中无法使用 javascript 进行修剪< /strong>。
Are you testing with IE? Are you getting any errors on the page?
If you take a look (I'm testing with chrome) here it works, it will allow you to press the button, it will show the error, enter something, click again and it alerts. So the button is still working.
However, IE doesn't work - it shows an error.....
.trim()
isn't a supported function by IE. So you can use jQuery's .trim() instead:Try again here it works with IE.
Take a look at Trim in javascript not working in IE.
您可以像这样处理验证过程
,因此可以多次调用验证,并且如果所有有效 - 表单都会被处理
You can handle validation process like this
so validation could be called more than once, and if all valid - form'll be processed
您可以使用“live”方法绑定到控件。但这通常用于动态创建新控件的情况。
you can use the 'live' method to bind to the control. but this is generally used in case new controls are dynamically created.
按下对话框按钮后,jQuery UI 不会取消绑定单击处理程序。
请参阅 http://jsfiddle.net/MwNJd/2/ 上的演示
您一定还有其他事情要做在。
jQuery UI doesn't unbind the click handler on a dialog button once it's pressed.
See a demo at http://jsfiddle.net/MwNJd/2/
You must have something else going on.