gpt4 book ai didi

javascript - jQuery - DOM 操作的结果不能分配给变量?

转载 作者:行者123 更新时间:2023-11-28 02:49:09 27 4
gpt4 key购买 nike

使用 jQuery,我通过某种过程更改输入文本字段的值。流程完成后,我需要捕获此更改并将其应用到另一个流程。我的问题是我似乎无法捕获此更改并将其分配给变量。我知道变化正在发生,因为 DOM 正在更新。此外,这个变量赋值在 IE 中有效,但不适用于我测试的其他浏览器。

下面是证明我的观点的片段(您可以在此处在线查看: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);
});

显然,我需要跨浏览器的一致行为。知道发生了什么吗?

最佳答案

我不知道会发生什么,但不知何故,将值设置为成员 (input.value) 或将值设置为属性节点之间应该存在差异。

这对我有用:

$('input[name=my_text]').each(function()           
{ this.setAttribute('value','Hello Text Field');});

我猜这是innerHTML中的一个错误,请参阅bugzilla:https://bugzilla.mozilla.org/show_bug.cgi?id=535992

关于javascript - jQuery - DOM 操作的结果不能分配给变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4162341/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com