javascript 使用 AJAX 发布输入 OnChange
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23712799/
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
Post Input OnChange with AJAX
提问by James Patrick
Just some AJAX troubleshooting.
只是一些 AJAX 故障排除。
Context:Building a large table with input that should post as soon as their filled out. Thought the onchange trigger would work best.
上下文:构建一个包含输入的大表,应在填写后立即发布。认为 onchange 触发器效果最好。
Issue:I can't seem to get the javascript to pass the value of the input over to the .php sheet.
问题:我似乎无法让 javascript 将输入值传递给 .php 表。
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
页面.php
<tr>
<td>
<input name="grp1" type="text" class="matchedit" onchange="postinput()">
</td>
</tr>
matchedit-data.php
matchedit-data.php
$entry = $_POST['matchvalue'];
$conn->query("UPDATE matches SET grp = '$entry' WHERE mid = 'm1'");
Thanks in advance!
提前致谢!
回答by undefined
For reading the value of a jQuery wrapped input .val()method should be used, valuehere returns an undefinedvalue. Also you should either use .on('change')or change()method, jQuery object doesn't have onchangemethod.
对于读取值,.val()应该使用jQuery 包装的输入法,value这里返回一个undefined值。此外,您应该使用.on('change')或change()方法,jQuery 对象没有onchange方法。
$(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');
});
});
});

