我正在尝试绑定(bind)按键(“enter”)以触发 jQuery 模态对象中的函数。在下面的代码中,我希望 $(this).dialog("login") 在检测到按键事件时触发。但是,我似乎无法调用 self.dialog("login")。我是不是看错了?
$("#login-dialog").dialog({
autoOpen: false,
height: 250,
width: 350,
modal: true,
open: function() {
var self = $(this);
$("#login-dialog").load("/accounts/login/", function() {
$("#id_username").focus()
.keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
self.dialog("login");
}
});
});
},
buttons: {
close: function() {
$(this).dialog("close");
},
login: function() {
$.ajax({
type: "POST",
url: "/accounts/login/",
data: $("#login-form").serialize(),
success: function(data) {
var errors = $(data).find("#login-error").val();
if (errors) {
$("#error-message").replaceWith("<p class='error'>" + "Your username and password didn't match" + "</p>");
} else {
window.location = "/builder";
}
}
});
}
}
});
最佳答案
快速解决您的问题,在“按键”功能内使用以下代码
$("#login-dialog").dialog("option").buttons.login();
关于javascript - jQuery UI 模态问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7503283/