使用AJAX发布输入OnChange [英] Post Input OnChange with AJAX
问题描述
只是一些AJAX故障排除。
Just some AJAX troubleshooting.
上下文:构建一个包含输入的大表,一旦填写完毕就应该发布。认为onchange触发器效果最好。
Context: Building a large table with input that should post as soon as their filled out. Thought the onchange trigger would work best.
问题:我似乎无法将javascript传递给输入的值.php表。
Issue: I can't seem to get the javascript to pass the value of the input over to the .php sheet.
header.php
$(document).ready(function(){
$(".matchedit").onchange(function postinput(){ // Problem 1: change(
var matchvalue = $(this).value; // Problem 2: $(this).val();
$.ajax
({
url: 'matchedit-data.php',
data: {matchvalue: matchvalue},
type: 'post'
});
});
});
page.php
<tr>
<td>
<input name="grp1" type="text" class="matchedit" onchange="postinput()">
</td>
</tr>
matchedit-data.php
$entry = $_POST['matchvalue'];
$conn->query("UPDATE matches SET grp = '$entry' WHERE mid = 'm1'");
提前致谢!
Thanks in advance!
推荐答案
为了读取jQuery包装输入的值 .val()
方法应该使用, value
此处返回 undefined
值。你也应该使用 .on('change')
或 change()
方法,jQuery对象没有 onchange
方法。
For reading the value of a jQuery wrapped input .val()
method should be used, value
here returns an undefined
value. Also you should either use .on('change')
or change()
method, jQuery object doesn't have onchange
method.
$(document).ready(function(){
$(".matchedit").on('change', function postinput(){
var matchvalue = $(this).val(); // this.value
$.ajax({
url: 'matchedit-data.php',
data: { matchvalue: matchvalue },
type: 'post'
}).done(function(responseData) {
console.log('Done: ', responseData);
}).fail(function() {
console.log('Failed');
});
});
});
这篇关于使用AJAX发布输入OnChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!