使用jQuery,我通過某個進程更改了輸入文本字段的值。完成該過程後,我需要捕獲此更改並將其應用於另一個進程。我的問題是,我似乎無法捕捉到這個變化並將其分配給一個變量。我知道這些更改正在發生,因爲DOM正在更新。此外,這個變量賦值在IE中可用,但不適用於我測試過的其他瀏覽器。jQuery - DOM操作的結果不能分配給變量?
下面是一個片段來證明我的觀點(你可以在這裏看到這個在線:http://jsfiddle.net/xMwAE/)。
<form>
<input type="hidden" name="my_hidden" value="Hidden Field" />
<input type="text" name="my_text" value="Text Field" />
</form>
$().ready(function() {
$('input[name=my_hidden]').val('Hello Hidden Field');
$('input[name=my_text]').val('Hello Text Field');
// Display
var temp = $('form').html();
// Though the DOM is updated with the new values. The variable temp
// does not capture the changes to the input text field, but captures
// the change in the hidden field. When in IE, temp captures the
// changes in both fields.
alert(temp);
});
顯然,我需要跨瀏覽器的一致行爲。任何想法發生了什麼?
你是一個天才!似乎工作,現在已經掙扎了好幾天。所以我必須使用setAttribute酷!非常感謝!啤酒?大聲笑 – Aman 2010-11-12 16:06:30
啤酒聽起來不錯^^ – 2010-11-13 14:51:10