jquery设置隐藏的输入值在IE7和IE8中无法正常工作 [英] jquery setting hidden input value not working as expected in IE7 and IE8

查看:179
本文介绍了jquery设置隐藏的输入值在IE7和IE8中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续采用我的代码来处理IE ...

Continuing adopting my code to work with IE...

我有一个包含表格的隐藏 div 编辑一些信息。当用户选择要编辑的项目时,将显示此div,并使用该项目的信息填充字段。 div(简化术语)看起来像这样:

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 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>

我使用jquery将值设置到字段中。我打开编辑div的功能如下所示:

I use jquery to set the values into the fields. My function for opening up the editing div looks something like this:

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();
    });
}

所有这些在FF,Opera,Webkit和IE 9中都能正常工作在IE7和IE8中,我遇到了一个奇怪的问题。我可以在 edit_item 函数中看到 item_id 正确设置,但是一旦该函数完成,隐藏的输入值( qid )重置为空字符串。当表单被ajax提交时,警报会将值显示为空字符串,尽管它已正确设置。有趣的是,所有其他领域都很好。它在IE 9中正常工作。

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.

我在这里缺少什么?非常感谢提前。

What am I missing here? Many thanks in advance.

推荐答案

这完全是愚蠢的,但事实并非如此:

This is totally stupid, and it shouldn't be the case, however:

$('#field1').val(item.property1);

不起作用。然而

$('#field1').attr("value", item.property1);

工作正常。我就离开了。

worked fine. I'm leaving it at that.

这篇关于jquery设置隐藏的输入值在IE7和IE8中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆