使用ajax出错


error using ajax

我有一个表(由php创建),其最后一列在每个单元格中都有输入框:php:

echo "<table id='booklist'><tr>
                         <th>Edit</th>
                         <th class='coursename'><a href='#' class='Course_Name'>Course Name</a></th>
                         <th class='startdate'><a href='#' class='Start_Date'>Start Date</a></th>
                         <th class='booktitle'><a href='#' class='Book_Title'>Book Title</></th>
                         <th class='bookauthor'><a href='#' class='Book_Author'>Book Author</a></th>
                         <th class='bookisbn'><a href='#' class='Book_Isbn'>Book ISBN</a></th>
                     </tr>";
        while($row = mysql_fetch_array($result))
        { 
            echo    "<tr>
                        <td><input type='checkbox'></input></td>
                        <td class='coursename'>" . $row['Course Name'] . "</td>
                        <td class='startdate'>" . $row['Start Date'] . "</td>
                        <td class='booktitle'>" . $row['Book Title']. "</td>
                        <td class='author'>" . $row['Book Author']. "</td>
                        <td class='isbn'><input class='ISBN_number' type='text' value='' size='13' maxlength='13'></input></td> 
                  </tr>";                   
        }               
echo "</table>";

我试图使一个jquery函数,做一个ajax调用来验证内容,如果有效,把一个复选标记的图片。下面是jquery:

//validates a manually inputed ISBN number   
$(document).ready(function() {
    $(".ISBN_number").change(function(){
        var isbnNum = $(this).val();
        console.log("isbnNum = " . isbnNum);
        $.get("validate_isbn.php", {isbn: isbnNum},
            function(answer) 
            {                   
                console.log(answer);
                if (answer == true)
                {
                    $(this).after("<img src='pics/green_checkmark.png' class='checkmark'>");
                }
                else 
                {
                }
            });
    });

ajax调用validate_isbn.php并返回true或false(我知道该文件有效,我在其他情况下使用过它)。但是当我在输入框中输入一个值时,什么也没有发生。没有我的控制台。日志打印出来,我得到的是以下消息:"event. log . log . log . log . log . log . log . log . log。"layerX和event。layerY在WebKit中被破坏和弃用。它们将在不久的将来从发动机中移除。"我不确定这意味着什么,我的代码有什么问题。

您的代码似乎有一些小错误。试试这个:

http://jsfiddle.net/mblase75/YfDPb/

$(document).ready(function() {
    $(".ISBN_number").change(function() {
        $this = $(this); // store the current 'this'
        var isbnNum = $this.val();
        console.log("isbnNum = "+isbnNum); // + not . to concatenate strings in JS
        $.get("validate_isbn.php", { // make sure this is the correct relative path
            isbn: isbnNum
        }, function(answer) {
            console.log($this);
            if (answer==='true') { // answer will be a string of text, not a boolean
                $this.after("<img src='pics/green_checkmark.png' class='checkmark'>");
            } else {
                $this.after("nope"); // to verify that something's happening
            }
        });// end callback
    });//end change
});//end ready

整个JS运行过程中发生错误。

安装FireBug并进入控制台选项卡查看错误。