Javascript 文本框失去焦点后如何调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11700453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a function after textbox lose focus
提问by Wallkan
I have no experience with Javascript/JQuery/AJAX so I'm trying to understand if it's possible to call a function that execute a query on my DB after a textbox lose focus.
我没有使用 Javascript/JQuery/AJAX 的经验,所以我试图了解是否可以在文本框失去焦点后调用在我的数据库上执行查询的函数。
I'm displaying a table in my page (using PHP) with text boxes that contains the same values of a table on my DB, and when someone change the value on a text box I want to change the DB table with an UPDATE query to make them equals; is that a way with AJAX or JQuery to do this?
我在我的页面中显示一个表(使用 PHP),其中包含与我的数据库中的表相同的值的文本框,当有人更改文本框中的值时,我想使用 UPDATE 查询将数据库表更改为使它们相等;这是 AJAX 或 JQuery 的一种方式吗?
回答by Sibu
<input type="text" id="check">?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$("#check").blur(function() {
alert('working');
});?
</script>
回答by spaceman12
Bind a handler to the change
event:
将处理程序绑定到change
事件:
<textarea onchange='ajax_call()'></textarea>
where ajax_call
is a function for updating the DB.
哪里ajax_call
是更新数据库的函数。
回答by HeM01
Yes, you can. First you need to wait for JavaScript event like onchange/onblur or other. Than you need to make an Ajax call to the server. It will change the data in db and can result some data, that you can put on the page. You can use jQuery to manipulate your page.
是的你可以。首先,您需要等待诸如 onchange/onblur 之类的 JavaScript 事件。比您需要对服务器进行 Ajax 调用。它会更改数据库中的数据并产生一些数据,您可以将其放在页面上。您可以使用 jQuery 来操作您的页面。
Another way is to wait until a person presses submit button and than update data in the db.
另一种方法是等到一个人按下提交按钮,然后更新数据库中的数据。