javascript jquery 设置隐藏输入值在 IE7 和 IE8 中无法按预期工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10654595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery setting hidden input value not working as expected in IE7 and IE8
提问by Aleks G
Continuing adopting my code to work with IE...
继续采用我的代码来处理 IE...
I have a hidden div
containing a form to edit some information. When the user selects the item to edit, this div is shown and the fields are populated with the information for the item. That divs (in simplified terms) looks like this:
我有一个隐藏的div
包含编辑一些信息的表单。当用户选择要编辑的项目时,将显示此 div 并使用该项目的信息填充字段。那个 div(在简化的术语中)看起来像这样:
<div id="editform">
<form action="" method="post" id="qform" name="qform">
First param: <input name="field1" id="field1"/> <br/>
Second param: <input name="field2" id="field2"/> <br/>
...
<input type="hidden" name="qid" id="qid" value=""/>
<img id="submit" src="..." alt="..." title="..." />
</form>
I use jquery to set the values into the fields. My function for opening up the editing div looks something like this:
我使用 jquery 将值设置到字段中。我打开编辑 div 的函数如下所示:
function edit_item(item_id) {
item = get_item(item_id); //this will return a JS object
$('#field1').val(item.property1);
$('#field2').val(item.property2);
...
$('#qid').val(item_id);
$('#submit').click(function() {
alert($('#qid').val());
$('#qform').ajaxSubmit();
});
}
All of this works fine in FF, Opera, Webkit and IE 9, however in IE7 and IE8, I'm having a strange problem. I can see the item_id
being set correctly in the edit_item
function, however as soon as that function completes, the hidden input value (qid
) gets reset to the empty string. When the form is being ajax-submitted, the alert shows the value to be an empty string despite it being set correctly. Interestingly, all other fields are fine. And it works correctly in IE 9.
所有这些在 FF、Opera、Webkit 和 IE 9 中都可以正常工作,但是在 IE7 和 IE8 中,我遇到了一个奇怪的问题。我可以看到函数item_id
中的设置正确edit_item
,但是一旦该函数完成,隐藏的输入值 ( qid
) 就会重置为空字符串。当表单被 ajax 提交时,警报显示该值是一个空字符串,尽管它设置正确。有趣的是,所有其他领域都很好。它在 IE 9 中正常工作。
What am I missing here? Many thanks in advance.
我在这里错过了什么?提前谢谢了。
回答by Aleks G
This is totally stupid, and it shouldn't be the case, however:
这完全是愚蠢的,但事实并非如此:
$('#field1').val(item.property1);
did not work. Yet
不工作。然而
$('#field1').attr("value", item.property1);
worked fine. I'm leaving it at that.
工作正常。我要离开了。
回答by user2401543
Solution for IE without JQuery in pure JavaScript does not look too complicated:
纯 JavaScript 中没有 JQuery 的 IE 解决方案看起来不太复杂:
document.getElementById(id).setAttribute('value', value);
回答by FatDog47
In addition to Aleks G's answer, I found out that value
attribute must notbe defined implicitly in the hidden element in order to jQuery .setAttr()
and .val()
work without issue in IE8.
除了 Aleks G 的回答之外,我发现不能在隐藏元素中隐式定义value
属性,以便 jQuery并在 IE8 中正常工作。.setAttr()
.val()
See here for more details: jQuery .val() setter not working?
有关更多详细信息,请参见此处: jQuery .val() setter 不起作用?