使用AJAX发布输入OnChange

只是一些AJAX故障排除。

上下文:构建一个包含输入的大表,一旦填写完毕就应该发布。 认为onchange触发器效果最好。

问题:我似乎无法将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文件

      

matchedit-data.php

 $entry = $_POST['matchvalue']; $conn->query("UPDATE matches SET grp = '$entry' WHERE mid = 'm1'"); 

提前致谢!

为了读取jQuery包装的输入值,应该使用.val()方法,这里的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'); }); }); });