JQuery selects the selected value method of the select component

亚连
Release: 2018-05-31 17:00:09
Original
1672 people have browsed it

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:

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

Get the selected value of select:

$(“#ddlregtype “).val();
Copy after login

Get the selected index of select:

$(“#ddlregtype “).get(0).selectedindex;
Copy after login

Set select :

Set select selected index:

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

Set select selected value:

(“#ddlregtype “).attr(“value”,”normal“);(“#ddlregtype “).attr(“value”,”normal“);(“#ddlregtype “).val(“normal”); $(“#ddlregtype “).get(0).value = value;
Copy after login

Set select selected text :

var count=$("#ddlregtype option").length; for(var i=0;i
        
Copy after login

Set the select option item:

$("#select_id").append(""); //添加一项option $("#select_id").prepend(""); //在前面插入一项option $("#select_id option:last").remove(); //删除索引值最大的option $("#select_id option[index='0']").remove();//删除索引值为0的option $("#select_id option[value='3']").remove(); //删除值为3的option $("#select_id option[text='4']").remove(); //删除text值为4的option
Copy after login

Clear the select:

$("#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:

//获取所有属性值 var item = $("#select1").val(); $(function(){ $('#select1').each( //获得select1的所有值 function(){ $('button').click(function(){ alert($('#select2').val()); //获得select2中的select1值 }); }); }) 
Copy after login

It is worth noting that it cannot be written directly as

$(function(){ $('#select2').each( //获得select1的所有值,因为前面讲选项从左边添加到右边,jquery其实并没有真正将值从左边传到右边。 function(){ $('button').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>>

<<

Use JQuery and Ajax to call the option of dynamically filling the Select Options

//绑定ClassLevel1单击事件 $("#ClassLevel1").change(function () { var id = $("#ClassLevel1").val(); var level2 = $("#ClassLevel2"); level2.empty(); $("#ClassLevel3").hide(); $.ajax({ url: "./askCommon.ashx?action=getclasslevel&pid=" + id, data: { "type": "ajax" }, datatype: "json", type: "get", success: function (data) { var json = eval_r(data); for (var ind in json) { level2.append($("")); } } }); })
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

vue.js moves the array position and updates the view at the same time

##vue.js or js implements Chinese A-Z Sorting method

Sample code of actual node static file server

The above is the detailed content of JQuery selects the selected value method of the select component. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!