jquery 操作select 总结

  1. $('this').find("option:selected").remove();   选中项删除
  2. var index=parseInt($("this").get(0).selectedIndex); 得到选中项的索引
  3. $('this').find("option:selected").val(); 得到select的value值
  4. $('this')[0].options(index).text;  根据索引得到select中的文本text
  5. $('this').find("option:selected").text(); 得到select的text文本
  6. $("#newitem option").each(function(){
                    options+=$(this).val();//select的value值遍历组合成字符串。
                    });
  7.  $.fn.size = function()
    {
        return $(this).get(0).options.length;
    }
  8.  $.fn.isExistItem = function(text)
    {
        var isExist = false;
        var count = this.size();
        for(var i=0;i<count;i++)
        {
            if($(this).get(0).options[i].text == text)
            {
                isExist = true;
                break;
            }
        }
        return isExist;
    }
  9. $.fn.addOption = function addOption(text,value)
    {
        if(this.isExistItem(text))
        {
            alert("待填值已经存在");
        }
        else
        {
            $(this).get(0).options.add(new Option(text,value));
        }
    }
  以上是jquery1.34对select的操作。还没总结完,有时间再说。

你可能感兴趣的:(jquery,select)