我有一个表(由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(我知道该文件有效,我在其他情况下使用过它)。但是当我在输入框中输入一个值时,没有任何反应。没有我的console.logs打印出来,我得到的是以下消息:“event.layerX和event.layerY在WebKit中被破坏和弃用。它们将在不久的将来从引擎中删除。”我不确定这意味着什么以及我的代码有什么问题。
答案 0 :(得分:1)
您的代码似乎遇到了一些小错误。试试这个:
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
答案 1 :(得分:-1)
整个JS运行期间发生了错误。安装FireBug并转到“控制台”选项卡以查看发生了什么问题。