Home Web Front-end JS Tutorial JQuery selects the selected value method of the select component

JQuery selects the selected value method of the select component

May 31, 2018 pm 05:00 PM
jquery select

Below I will share with you an article about the value method of selecting the selected component in JQuery. It has a good reference value and I hope it will be helpful to everyone.

jquery gets the text and value selected by select

Get select:

Get the selected text of select:

1

$(“#ddlregtype”).find(“option:selected”).text();

Copy after login

Get the selected value of select:

1

$(“#ddlregtype “).val();

Copy after login

Get the selected index of select:

1

$(“#ddlregtype “).get(0).selectedindex;

Copy after login

Set select :

Set select selected index:

1

$(“#ddlregtype “).get(0).selectedindex=index;//index为索引值

Copy after login

Set select selected value:

1

2

(“#ddlregtype “).attr(“value”,”normal“);(“#ddlregtype “).attr(“value”,”normal“);(“#ddlregtype “).val(“normal”);

$(“#ddlregtype “).get(0).value = value;

Copy after login

Set select selected text :

1

2

3

4

5

6

7

8

9

var count=$("#ddlregtype option").length;

 for(var i=0;i<count;i++)

   {      if($("#ddlregtype ").get(0).options[i].text == text)

    {

      $("#ddlregtype ").get(0).options[i].selected = true;

      break;

    }

  }

$("#select_id option[text=&#39;jquery&#39;]").attr("selected", true);

Copy after login

Set the select option item:

1

2

3

4

5

6

$("#select_id").append("<option value=&#39;value&#39;>text</option>"); //添加一项option

$("#select_id").prepend("<option value=&#39;0&#39;>请选择</option>"); //在前面插入一项option

$("#select_id option:last").remove(); //删除索引值最大的option

$("#select_id option[index=&#39;0&#39;]").remove();//删除索引值为0的option

$("#select_id option[value=&#39;3&#39;]").remove(); //删除值为3的option

$("#select_id option[text=&#39;4&#39;]").remove(); //删除text值为4的option

Copy after login

Clear the select:

1

$("#ddlregtype ").empty();

Copy after login

Work needs to get the values ​​​​in the two forms. As shown in the picture:

How to get the value added from the left selection box to the right selection box? I thought about using web page special effects to get it, and the more popular jquery was used here.

js code is as follows:

1

2

3

4

5

6

7

8

9

10

//获取所有属性值 var item = $("#select1").val();

$(function(){

 $(&#39;#select1&#39;).each( //获得select1的所有值

   function(){

    $(&#39;button&#39;).click(function(){

      alert($(&#39;#select2&#39;).val()); //获得select2中的select1值

    });

   });

})

</script>

Copy after login

It is worth noting that it cannot be written directly as

1

2

3

4

5

6

7

8

$(function(){

 $(&#39;#select2&#39;).each( //获得select1的所有值,因为前面讲选项从左边添加到右边,jquery其实并没有真正将值从左边传到右边。

   function(){

    $(&#39;button&#39;).click(function(){

      alert($(this).val()); //获得select2中的select1值

    });

   });

})

Copy after login

html:

Option 1
Option 2
Option 3
Option 4
Option 5
Option 6
Option 7

Select to add to the right>>
Add all to the right>>

<