jq select 操做

<select disabled="disabled" name="typename" class="form-field  col-sm-12 selecttype  ">
             
              
               <option value="2">重点工做</option>
                <option value="1">OKR</option>
             </select>
 jquery

// select 设置选中值
        $(".selecttype").val(data.type + "");app

 

能够参考 form 的 操做select 代码:this

form.find('select').each(function() {
            var name = $(this).attr('name');
            try {
                if (name == p && name != breakFields) {
                    var value = result[p].toString();
                    if (isNotEmpty(value)) {
                        //console.log(name+" select value:"+value);
                        if (value.indexOf(",") != -1) {
                            var vals = value.split(",");
                            for (var dex = 0; dex < vals.length; dex++) {
                                //console.log(" select dex:"+dex+",val:"+vals[dex]);
                                $(this).find("option[value=" + vals[dex] + "]").prop('selected', true);
                            }
                        } else {
                            $(this).find("option[value=" + value + "]").prop('selected', true);
                        }
                    }
                    isContinue = true;
                    return false;
                }
            } catch (e) {
                console.log(e);
            }
        });.net

 

参考地址:orm

http://blog.csdn.net/nairuohe/article/details/6307367blog

每一次操做select的时候,老是要出来翻一下资料,不如本身总结一下,之后就翻这里了。get

好比<select class="selector"></select>it

一、设置value为pxx的项选中io

     $(".selector").val("pxx");console

二、设置text为pxx的项选中

    $(".selector").find("option[text='pxx']").attr("selected",true);

    这里有一个中括号的用法,中括号里的等号的前面是属性名称,不用加引号。不少时候,中括号的运用能够使得逻辑变得很简单。

三、获取当前选中项的value

    $(".selector").val();

四、获取当前选中项的text

    $(".selector").find("option:selected").text();

    这里用到了冒号,掌握它的用法并触类旁通也会让代码变得简洁。

 

不少时候用到select的级联,即第二个select的值随着第一个select选中的值变化。这在jQuery中是很是简单的。

如:$(".selector1").change(function(){

     // 先清空第二个

      $(".selector2").empty();

     // 实际的应用中,这里的option通常都是用循环生成多个了

      var option = $("<option>").val(1).text("pxx");

      $(".selector2").append(option);

});

相关文章
相关标签/搜索