通过使用onblur启动Ajax查询到PHP MySQL DB

kicking off ajax query to php mysql DB by use of onblur

本文关键字:查询 PHP MySQL DB Ajax 启动 onblur      更新时间:2023-09-26

当人们离开表单输入字段时,我正在尝试(但失败)使ajax进程工作。我需要它发生在表单上的任何类型的每个输入字段上。

我正在尝试修改以下内容(确实有效):

$("document").ready(function() {
    $("#getcontent").click(getContent);
});
function getContent() {
    $("#example").load("sampletextcontent.txt");
}
(HTML 中会有一个带有 id="

getcontent" 的按钮,在 HTML 中也会有一个带有 id="example" 的div。单击按钮时,外部文件示例文本内容的内容.txt显示在所述div中)

正在使用jquery U,版本2.0.3 jquery.min.js

所以我正在尝试(这就是我失败的地方)是将上述内容转换为:

$("document").ready(function() {
    $("#input_1_1").onblur(doSend);
    $("#input_1_2").onblur(doSend);
    $("#input_1_3").onblur(doSend);
    $("#input_1_4").onblur(doSend);
    $("#input_1_5").onblur(doSend); // etc for as many fields there are
})
function doSend() {
    // Do some ajax stuff to send the entire value of all form fields in here
}

但它似乎不喜欢在这里使用".click"替换为".onblur"的概念。这是为什么呢?onblur 不是有效的 JS 函数/项吗?

对不起,我不是JS大师,我在理解JS方面遇到了很多问题。

C

编辑 - 对不起,我不清楚我试图工作的代码。我想工作的版本中没有按钮,它只是想在用户单击/按 Tab 键离开每个输入字段时触发。很抱歉之前没有说清楚。

对于动态jQuery事件绑定,我会尝试使用.on函数切换.click和.blur函数。

例如,我会尝试以下方法:

$('body').on('click', '#getcontent', function(){
    DoSomething();
});

$('body').on('blur', '#input_1_1', function(){
    DoSomething();
});

on 函数的文档可以在 http://api.jquery.com/on/找到。

这是另一篇 Stack Overflow 文章,也对此进行了解释:动态创建元素上的事件绑定?。

感谢评论和回答尝试。但是,我正在使用的答案,实际上确实回答了特定问题,如下所示。只需更改:

$("document").ready(function() {
    $("#input_1_1").onblur(doSend);
    $("#input_1_2").onblur(doSend);
    $("#input_1_3").onblur(doSend);
    $("#input_1_4").onblur(doSend);
    $("#input_1_5").onblur(doSend); // etc for as many fields there are
})

成为:

$("document").ready(function() {
    $("#input_1_1").blur(doSend);
    $("#input_1_2").blur(doSend);
    $("#input_1_3").blur(doSend);
    $("#input_1_4").blur(doSend);
    $("#input_1_5").blur(doSend); // etc for as many fields there are
})

它有效。如果我愿意,这保留了为每个字段调用不同函数的能力,并且对于像我这样的 JS 新手来说非常简单。

以后可能会实施更好的更清洁的解决方案,但现在这可以并直接回答原始问题。