フォームのインプレース編集に取り組んでいます。私は2つのdivを持っています。そのうちの1つは表示要素を保持し、もう1つは入力フォームを保持しています。
編集データをクリックすると、表示 div から入力フォームに移動します。val(text) を使用すると変化が見られますが、フォームを json 要素にシリアル化すると古い要素になります。
ここで何が問題なのかを理解するのに助けが必要ですか?
ここにいくつかのコードがあります:
function editForm2(){
$("#editLink").click(function() {
if (this.text == 'Edit') {
console.log('editing');
$("#display div.edit").each(function(i) {
var e = $("#input :input")[i];
$(e).val($(this).text());
});
$("#display").hide();
$("#input").show();
$(this).text('Save');
}
else if (this.text =='Save') {
// problem is here... When I serialize the form I got nothing ?!
console.log('saving');
console.log($("#form1 :input")); // old values
var json_form = $('#form1').serializeObject();
console.log(json_form); // old values?
$(this).text('Edit');
$("#display").show();
$("#input").hide();
}
});
console.log(this);
}
$(document).ready(function() {
editForm2();
});
ここにhtmlがあります
<div class="editSection" id="display" >
<div id="person_firstName" class="edit" width="200">
Hello
</div>
<div id="person_lastName" class="edit">
World
</div>
</div>
<div class="editSectionEdit" id="input" >
<form id="form1">
<input name="person_firstName_in" type="text" class="edit" value="123" >
<input name="person_lastName_in" type="text" class="edit" value="456" >
</form>
</div><br/>
<div id="buttons">
<a href="#" id="editLink">Edit</a>
<a href="#" id="cancelLink">Cancel</a>
</div><br>
<pre id="result"></pre>