如何在 jquery 对话框中运行 JavaScript 代码
我打开一个 jquery 对话框,它打开得很好,该对话框的内容是一个表单,该表单的一个输入是:
<input onkeyup="testOnKeyUp();" type="text" name="userName" id="userName">
这是我的问题所在,假设当有人在此输入中键入 Key 时,将激活以下代码并且应该运行,但它就是不起作用。
var validateUserNameSpan = $j('#userNameSpan');
function testOnKeyUp(){
validate(this, validateUserNameSpan, 'username');
}
function validate(field, span, property) {
if (field.value != field.lastValue) {
if (field.timer) clearTimeout(field.timer);
field.timer = setTimeout(function () {
span.value = "";
span.removeClass('error').html('checking ...');
$j.ajax({
url: '/signup/'+property,
data: property + '=' + field.value,
dataType: 'json',
type: 'post',
success: function (json) {
updateMessage(span, json.success, json.message);
if(property=="email"){
emailValid = true;
}else if(property=="username"){
userNameValid = true;
}
},
error: function() {
ajFailureInitSignup(span);
if(property=="email"){
emailValid = false;
}else if(property=="username"){
userNameValid = false;
}
}
});
}, 400);
if(userNameValid && emailValid) document.getElementById('buttonLink').onclick=null ;
field.lastValue = field.value;
}
}
我做错了什么?
我不知道,但谷歌搜索我得到的信息让我觉得:
$j.ajax({
我在这个对话框中运行的(验证函数)没有被执行,为什么我这么说?因为这个对话框是在 DOM 中创建的,并且在创建之后 $j.ajax 不会被执行。
这是真的吗?
我真的很感谢任何帮助。
I open a jquery dialog, it opens very good, the content of this dialog is a form, one input of that form is:
<input onkeyup="testOnKeyUp();" type="text" name="userName" id="userName">
Here is where come my problem, it's suppose that when someone type a Key in this input the following code is activated and should run, but it just does not work.
var validateUserNameSpan = $j('#userNameSpan');
function testOnKeyUp(){
validate(this, validateUserNameSpan, 'username');
}
function validate(field, span, property) {
if (field.value != field.lastValue) {
if (field.timer) clearTimeout(field.timer);
field.timer = setTimeout(function () {
span.value = "";
span.removeClass('error').html('checking ...');
$j.ajax({
url: '/signup/'+property,
data: property + '=' + field.value,
dataType: 'json',
type: 'post',
success: function (json) {
updateMessage(span, json.success, json.message);
if(property=="email"){
emailValid = true;
}else if(property=="username"){
userNameValid = true;
}
},
error: function() {
ajFailureInitSignup(span);
if(property=="email"){
emailValid = false;
}else if(property=="username"){
userNameValid = false;
}
}
});
}, 400);
if(userNameValid && emailValid) document.getElementById('buttonLink').onclick=null ;
field.lastValue = field.value;
}
}
What am I doing wrong ?
I don't know but googling I've got info that make me feel that the:
$j.ajax({
that I'm running inside of this dialog (validate function) is not being executed, why I say that ? because this dialog is created in the DOM and after be created there this $j.ajax is not executed.
is this true ?
I really thank any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,主要问题是您的 AJAX 调用在发出后立即返回 - 这是正常的(这是 ajax 中的第一个“A” - 异步)。这意味着您的代码立即继续到
if (usernamevalid && emailvalid)
代码部分。这些变量此时并不存在,因为 AJAX 调用尚未返回。所以你正在比较未定义的变量,这也会失败。您必须将该比较移至 ajax 成功/错误处理程序内,以便仅当 ajax 调用实际生成可比较的数据时才会进行比较。
Ok, the main problem is that your AJAX call returns IMMEDIATELY after you issue it - this is normal (it's the first 'A' in ajax - asynchronous). That means your code continues on immediately to the
if (usernamevalid && emailvalid)
code section. Those variables do not exist at that point, as the AJAX call has not yet returned. So you're comparing undefined variables, which will also fail.You have to move that comparison inside your ajax success/error handlers, so that the comparison will only be done when the ajax call has actually produced data that can be compared.