如何仅在提交时获取表单输入的值?我有这个..
<form id="addCommentForm<?PHP echo $PostID;?>" method="post" action="">
<input type="hidden" value="<?PHP echo $PostID;?>" name="comentonpost" id="comentonpost"/>
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea>
<input type="submit" id="submit" value="Submit" />
</form>
$PostID
是数字,在页面中打印的每个表单中都是不同的,因此页面上的所有表单名称都不同。在 HTML 中,它看起来像这样:
打印的第一个表格:
<form id="addCommentForm3" method="post" action="">
<input type="hidden" value="3" name="comentonpost" id="comentonpost"/>
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea>
<input type="submit" id="submit" value="Submit" />
</form>
第二次打印:
<form id="addCommentForm2" method="post" action="">
<input type="hidden" value="2" name="comentonpost" id="comentonpost"/>
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea>
<input type="submit" id="submit" value="Submit" />
</form>
我comentonpost
在 javascript 中获取 id 值,如下所示:
$(document).ready(function(){
var name_element = document.getElementById('comentonpost');
var x = name_element.value;
});
我需要这个值,所以下面的脚本将把新的评论放在 #addCommentContainer'+x
这是整个javascript:
$(document).ready(function(){
var name_element = document.getElementById('comentonpost');
var x = name_element.value;
/* The following code is executed once the DOM is loaded */
/* This flag will prevent multiple comment submits: */
var working = false;
/* Listening for the submit event of the form: */
$('#addCommentForm'+x).submit(function(e){
e.preventDefault();
if(working) return false;
working = true;
$('#submit').val('Working..');
$('span.error').remove();
/* Sending the form fileds to submit.php: */
$.post('comment.submit.php',$(this).serialize(),function(msg){
working = false;
$('#submit').val('Submit');
/*
/ If the insert was successful, add the comment
/ below the last one on the page with a slideDown effect
/*/
$(msg.html).hide().insertBefore('#addCommentContainer'+x).slideDown();
},'json');
});
});
但是通过这种方式,它获取了打印的第一个表单的值。如何comentonpost
仅在提交的表单上获取值?