送信時にのみフォームの入力の値を取得するにはどうすればよいですか? 私はこれを持っています..
<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>
2 番目の印刷:
<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
送信されたフォームのみの値を取得するにはどうすればよいですか?