<div id="div1">
<select>
<option value="1" >1</option>
<option value="2" selected="selected" >2</option>
<option value="3" >3</option>
</select>
</div>
<div id="div2"></div>
<input id="btn1" value="btn" onclick="copySelect" />
function copySelect()
{
var template=$("#div1").find("select").clone
template.find("option").removeAttr("selected");
template.find("option[value='3']").attr("selected","true");
$("#div2").html(template.html);
}
发现以上操作在IE中是正常的,点击按扭时,在div2中的select默认选中是3
但是在firefox中attr("selected","true");没有效果,这是为什么呢?

解决方案 »

  1.   

    其实你随便写个selected1,jquery也会给你执行。与jqeury无关的事情,就不用到jquery里边找原因了。
      

  2.   

    不用这些设置吧。直接给select标签一个名字,比如:selAbc选中哪一项就是:$("#selAbc").val(“这里填要选中项的value值”);
      

  3.   


    你把jquery这个选择项写复杂了。。直接给select一个ID不用去循环里面的项。直接把要选中项的value赋给这个select的val
      

  4.   


    var template=$("#div1").find("select").clone();
    template.find("option").removeAttr("selected");
    template.find("option[value='3']").attr("selected","true");
    $("#div2").html(template);
    分多 ?一个这东西到处发 。
      

  5.   


    <!--ie ff都有效-->
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#Button1").click(function(){
                    $("#Select1 option").eq(2).attr("selected",true);
                })
            })
        </script>
    </head>
    <body>
        <select id="Select1">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
        <input id="Button1" type="button" value="button" />
    </body>
    </html>
      

  6.   


    要求是,复制一个原select,删除其中原option的选中状态,更改为另一个option为选中状态,
    然后再添加到页面上~
      

  7.   

    既然楼主说了要求,实现方法有很多
    先上代码
    http://jsfiddle.net/pDGZR/
      

  8.   

    <html>
    <head id="Head1" runat="server">
        <title>无标题页</title>
        <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#Button1").click(function(){
                    var clone = $("#Select1").clone();
                    $("#Select1 option").eq(2).attr("selected",true);
                    $("body").append(clone);
                })
            })
        </script>
    </head>
    <body>
        <select id="Select1">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
        <input id="Button1" type="button" value="button" />
    </body>
    </html>
      

  9.   


    谢谢这位兄弟,可以是我还没说明白我的要求,因为添加操作不是当前进行的,
    它的逻辑是,
    1 先把拷贝的select选中项选中状态删除,
    2 然后再改变成另一个选中项
    3 再把2步的结果存储到另一个对象中,
    4 在某些条件触发时,再根据某些条件动态生成它的ID,再插入页面中的某个位置 因为它在当前交不能确定ID,和未插入页面,不能进行展现到页面后再修改其选中值的操作。
    我希望能在存储时用
    template.find("option[value='3']").attr("selected","true");
    将其选中状态设置好,然后保存到某地(还未插入)
    但上句在IE中可以正常使用,但在Firefox中不行,
    实在想不通原因,希望大家帮忙
      

  10.   


    <!--还是不太明白lz的意思,我就直接拿你的代码来改了。ie ff都运行可以-->
    <html>
    <head id="Head1" runat="server">
        <title>无标题页</title>
        <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
        <script type="text/javascript">
            function copySelect()
            {
                var template=$("#div1").find("select").clone();
                //template.find("option").removeAttr("selected");//这句没必要加,因为你指定了另外一个为 选中状态了,select只能有一个为选中状态
                template.find("option[value='3']").attr("selected","true");
                $("#div2").html(template);
            }
        </script>
    </head>
    <body>
        <div id="div1">
            <select>
                <option value="1">1</option>
                <option value="2" selected="selected">2</option>
                <option value="3">3</option>
            </select>
        </div>
        <div id="div2">
        </div>
        <input id="Button1" type="button" value="button" onclick="copySelect()"/>
    </body>
    </html>
      

  11.   


    谢谢这位兄弟,在你的代码的提示下,我好像找到了一点线索。
    1. 
    //template.find("option").removeAttr("selected");//这句没必要加,因为你指定了另外一个为 选中状态了,select只能有一个为选中状态
    这句不能去除,因为这是个demo,而在实际环境中是一段比较复杂的Dom树,其中有多个select,而除了特定的select,其它的必需清除选中状态,
    2. 你的代码确实可以,是我的例子弄的不好,你把$("#div2").html(template);
    换成alert(template.html());就知道我的疑惑了,
      

  12.   

     <OPTION value=1>1</OPTION> <OPTION value=2>2</OPTION> <OPTION value=3 selected>3</OPTION>/*输出之后显示上面那个,有什么问题?还是你是说
    template.find("option:selected").removeAttr("selected");这句效果没去掉?
    */
        <script type="text/javascript">
            function copySelect()
            {
                var template=$("#div1").find("select").clone();
                template.find("option:selected").removeAttr("selected");
                template.find("option[value='3']").attr("selected","true");
                $("#div2").html(template);
                document.write(template.html());
            }
        </script>
      

  13.   

    function copySelect() {
                var template = $("#div1").find("select").clone();            template.find("option").removeAttr("selected");//这句没必要加,因为你指定了另外一个为 选中状态了,select只能有一个为选中状态            template.find("option[value='3']").attr("selected", "true");            alert(template.html());
                
            }<div id="div1">
            <select>
                <option value="1">1</option>
                <option value="2" selected="selected">2</option>
                <option value="3">3</option>
            </select>
        </div>
        <div id="div2">
        </div>
        <input id="btn1" value="btn" onclick="copySelect();" />
    把JS换成上面的,然后在firefox中运行下,看alert出的内容,selected没有出现,
    (因为我不是要当时插入dom树,而是先保存它)
      

  14.   


    <!--喔 你是说在ff下弹出那个html(),3没有selected这个属性?下面已经改了,ie ff都有那个属性显示-->
    <html>
    <head id="Head1" runat="server">
        <title>无标题页</title>
        <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
        <script>
            function copySelect() {
                var template = $("#div1").find("select").clone();
                template.find("option").removeAttr("selected");
                //template.find("option[value='3']").attr("selected", "selected");
                template.find("option[value='3']")[0].setAttribute("selected","selected");
                alert(template.html());
                $("#div2").html(template);
            }
        </script>
    </head>
    <body>
        <div id="div1">
            <select>
                <option value="1">1</option>
                <option value="2" selected="selected">2</option>
                <option value="3">3</option>
            </select>
        </div>
        <div id="div2">
        </div>
        <input id="Button1" type="button" value="button" onclick="copySelect()"/>
    </body>
    </html>
      

  15.   

    虽说jquery兼容很好,不过也不可能做得完美,js如果只对dom操作的话各个浏览器是兼容的
      

  16.   


    下面还有关于这个问题的其它区的帖子,进来留个言,我好结贴。
    http://topic.csdn.net/u/20110305/15/36dd0405-cfc3-4fb4-965c-277e378e0f08.htmlhttp://topic.csdn.net/u/20110305/15/efc63f49-a231-4ce6-9f7a-0a1907399ace.html
      

  17.   

    template.find("option[value='3']")[0].setAttribute("selected","selected");
    这句的[0]是把它转为dom元素,然后进行设置属性
      

  18.   

    $("#select1").find("option[value=3]").attr("selected",true)