分配文本和值以在jQuery中选择

时间:2015-01-13 10:16:33

标签: jquery

我有两个 td 。在每个td中都有选择,其中包含n 选项。我想为select中的每个第一个选项分配不同的文本和值。

这是我试过的:

<script type="text/javascript" charset="utf-8">
    $( document ).ready(function() {

        var c = 1;
        $("td select").each(function(){
            if (c == 1){
                console.log(this);
                $(this + " option:nth-child(1)").val("-1");
                $(this + " option:nth-child(1)").text("Choose role");
            }else if(c == 2){
                $(this + " option:nth-child(1)").val("-1");
                $(this + " option:nth-child(1)").text("Choose status");
            }
            c++;
        });
    });
</script>

它没有做任何事情。我错过了什么?

1 个答案:

答案 0 :(得分:0)

您需要使用

 $(this).find("option:nth-child(1)")

而不是

 $(this + " option:nth-child(1)")