これは完璧に動作します!;)
これは、Ajaxを使用して、私が "フォームミラーエレメント"と呼ぶ方法で実行できます。代わりに、要素のあるフォームを外部に送信する代わりに、偽のフォームを作成できます。以前のフォームは必要ありません。
<!-- This will do the trick -->
<div >
<input id="mirror_element" type="text" name="your_input_name">
<input type="button" value="Send Form">
</div>
コードajaxは次のようになります:
<script>
ajax_form_mirror("#mirror_element", "your_file.php", "#your_element_response", "POST");
function ajax_form_mirror(form, file, element, method) {
$(document).ready(function() {
// Ajax
$(form).change(function() { // catch the forms submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: method, // GET or POST
url: file, // the file to call
success: function (response) { // on success..
$(element).html(response); // update the DIV
}
});
return false; // cancel original event to prevent form submitting
});
});
}
</script>
これは、親フォームを送信せずに別のフォーム内にデータを送信する場合に非常に役立ちます。
このコードはおそらく必要に応じて適応/最適化できます。それは完全に動作します!! ;)次のような選択オプションボックスが必要な場合にも機能します。
<div>
<select id="mirror_element" name="your_input_name">
<option id="1" value="1">A</option>
<option id="2" value="2">B</option>
<option id="3" value="3">C</option>
<option id="4" value="4">D</option>
</select>
</div>
それが私のような誰かを助けてくれたことを願っています。;)