我有一段javascript在JQuery 1.7下运行得很好,但在1.8下它不合作。基本上我有一个全局变量,用于限制人们在函数内的2条注释:
<script src="scripts/jquery-1.8.1.min.js"></script>
<script language="javascript" type="text/javascript">
var postNumber = 0;
function sendComment() {
var formData = $('#commentForm').serialize();
var url = "photo.php";
if ($("#comment").val() == "") {
$("#interactionResults").html(' Please type something.').show().fadeOut(6000);
} else {
if (postNumber < 2) {
$("#pmFormProcessGif").show();
$.post(url, formData, function(data) {
var returnString = data;
if (returnString.match(/^Error:/)) {
$("#interactionResults").html(data).show().fadeOut(6000);
} else if (postNumber == 0) {
$('#newCommentDisplay').html(returnString).show();
} else if (postNumber == 1){
$('#newCommentDisplay2').html(returnString).show();
}
document.commentForm.comment.value='';
$('#pmFormProcessGif').hide();
++postNumber;
}); // end post
} else {
$('#interactionResults').html("<p>To combat spam, we only allow 2 posts at a time.<br />Please refresh your browser and try again. Thank you.<p>").show();
}//end if postNumber
}
}
</script>
代码仍然有效,但由于某些奇怪的原因,它不再将postNumber增加到1。这是html部分:
<form action="javascript:sendComment();" method="post" name="commentForm" id="commentForm">
<textarea name="comment" cols="35" rows="5" id="comment"></textarea><br />
<input type="hidden" name="photo" id="photo" value="<?php echo $getID; ?>" />
<input type="submit" name="button" id="button" value="Submit"/>
<span id="pmFormProcessGif" style="display:none;"><img src="images/loading.gif" width="28" height="10" alt="Loading" /></span>
</form>
<div class="commentDisplayList">
<div id="newCommentDisplay2"></div>
<div id="newCommentDisplay"></div>
</div>