使用AJAX发布输入OnChange

时间:2014-05-17 15:02:50

标签: javascript php jquery ajax

只是一些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文件

<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'");

提前致谢!

1 个答案:

答案 0 :(得分:7)

为了读取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');
        });
    });
});