Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

javascript - jquery setting hidden input value not working as expected in IE7 and IE8

Continuing adopting my code to work with 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 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:

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.

What am I missing here? Many thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...