English 中文(简体)
IE6 not changing attributes (id and name) with jQuery attr call - any suggestions?
原标题:

Suggestions other that dumping IE6 that is... I can t control that :)

Quick synopsis of what I am trying to do: I have a form with select elements. When the user selects a specific option in the select (id type2 in the code below), I display a div structure that contains a new select. I want the new div structure and its children to be a clone of an existing one (id pdProgramDiv1) because I use php to populate the select in the div. Once the select is cloned, I need to change the clone s id and name of the div and its child label and select because the form information is getting passed to my submit php script. If they don t have unique ids, then although they are serialized into the query string, the $_POST array in php only includes the last value of a given id.

I have included the code that works absolutely perfectly in Firefox sigh, and fails to change the ids and names in IE6. This code may not be as concise as some; I am still pretty new to jQuery.

$( #type2 ).change(function() {
    if ($( #type2 option:selected ).text() ==  Program Director ) {
        $( #pdProgramDiv1 ).clone().attr({id:  pdProgramDiv2 , name:  pdProgramDiv2 }).appendTo($( #type2Div ));

        $( #pdProgramDiv2 > #pdProgram1 ).attr({id:  pdProgram2 , name:  pdProgram2 });

        $( #pdProgramDiv2 > label ).attr( for ,  pdProgram2 );
    } else {
        $( #pdProgramDiv2 ).hide();
    }
}); 

I have looked around, and tried using variables rather than chaining, and individual calls to attr rather than using the object literal (or map), but nothing helps. All the variations work fine in FF, but no joy in IE6.

Is this a known problem, or can anyone tell me way to make the changes happen?

If I can t get this to work, I guess I will have to create all the elements and hide them, but that doesn t seem as good.

Thanks in advance as always!

UPDATE: SOLVED

Thanks so much to the folks that found the error of my ways. I wanted to post the code that ending up solving the problem. As I said in my comment to the accepted answer below, the fix makes perfect sense. A little embarassed I didn t see it, but sometimes a fresh pair of eyes is needed! The only change is with selecting the child select rather than the select ID, and removing the name attr assignment on div, because as mentioned, it does nothing.

$( #type2 ).change(function() {
    if ($( #type2 option:selected ).text() ==  Program Director ) {
        $( #pdProgramDiv1 ).clone()
            .attr( id ,  pdProgramDiv2 )
            .appendTo($( #type2Div ));

        $( #pdProgramDiv2 > select ).attr({
             id:  pdProgram2 , 
             name:  pdProgram2 });

        $( #pdProgramDiv2 > label ).attr( for ,  pdProgram2 );
    } else {
        $( #pdProgramDiv2 ).hide();
    }
}); 
最佳回答

I think you problem is here...

$( #type2 ).change(function() {
    /* snip */
        $( #pdProgramDiv1 ).clone().attr({id:  pdProgramDiv2 , name:  pdProgramDiv2 }).appendTo($( #type2Div ));

        $( #pdProgramDiv2 > #pdProgram1 ).attr({id:  pdProgram2 , name:  pdProgram2 });
    /* snip */
});

You re cloning the container, switching it s ID, and inserting it back into the page. The containers are fine, but the page now cotnains two elements ID d #pdProgram1. document.getElementById is not going to like that.

Refactor the code to search for the select element by its tag instead of by its ID.

问题回答

I believe you ve just crashed in a problem with IE6, because it handles poorly the node cloning, if at all. It precisely fails on fixing the name and id of the clonned elements...

More info here.





相关问题
IE6 floated element wrapping problеm

I am trying to get tags to wrap to the next line by left floating them. In firefox the text will wrap onto the start of the next line, however IE6 will wrap the text onto the line directly under the ...

IE color <dt> spill

I have made a page here the DT title is underline red with text white... in firefox everything is fine... but the dt spill in IE... why ? here is a VIEW of the problem ! Here is the perfect example ...

Website surfacing blank pages in IE6

- Since discovering more about my problem I have modified my question A single user is complaining that (on XP and using IE6) they re not able to follow any links around my site. The behaviour they ...

Why are some images rendering on IE6 and some are not?

the images are showing up in safari and firefox but not IE6. I have four images, two of them are showing up in IE6 and two are not. If it helps, they are being toggled with an if statement. ...

IE6 positioning issue

It works as intended in Safari, Firefox etc. But it is not reading the positioning or z-index in IE6. How can I make it layout as it should in IE6? Here is the CSS: .AuthorName_Pic { width: ...

Nasty IE6 <ul> <li> stair bug

In that page, the submenu if fine in firefox, but as usual, the nasty explorer is interpreting that differently.... how do you troubleshoot css bug in IE6 what is the css bud there ? here is the ...

radio button jquery ie6 problem

i am dynamically creating radio using jquery as shown belown. but they value only selected in ff,ie8. ie6,ie7 not selecting the values. how can i select the radio button value in ie6/7. rand=$("<...

How can I make my fixed position work in IE6?

I ve have tried this: body {height: 100%;overflow: auto; body #cornerImage {position: absolute;bottom: 0;} and this: { margin:0; padding:0; } html, body { height: 100%; overflow:auto; } ...

热门标签