Home Web Front-end JS Tutorial Summary of common methods for setting and obtaining Select

Summary of common methods for setting and obtaining Select

Jun 19, 2017 pm 04:16 PM
select Commonly used method Obtain set up

1. Get the text selected by select:
$("#cusChildTypeId").find("option:selected").text();
$("#cusChildTypeId option:selected").text ()

2. Get the value selected by select:
$("#ddlRegType ").val();

3. Get the index selected by select :
$("#ddlRegType ").get(0).selectedIndex;

4. Get the number of selected items
$("#cusChildTypeId").get(0).options .length

5.Set the selected index of select:
$("#cusChildTypeId").get(0).selectedIndex=index;//index is the index value

6. Set the selected value of select:
$("#cusChildTypeId").attr("value","Normal");
$("#cusChildTypeId").val("Normal");
$ ("#cusChildTypeId").get(0).value = "Normal";

7.Set select selected text:
1>.

var count=$("#cusChildTypeId").get(0).options.length;
 for(var i=0;i<count;i++)  
 {           
  if($("#cusChildTypeId").get(0).options.text == text)  
   {  
   $("#cusChildTypeId").get(0).options.selected = true;
   break;  
   }  
 }
Copy after login

2>.

$("#cusChildTypeId").val(text);
$("#cusChildTypeId").change();
Copy after login

8. Add an item to the select, the display content is text, the value is value
$("#cusChildTypeId").get(0).options.add(new Option(text,value)) ;

9.Delete items with value in select

var count = $("#cusChildTypeId").size();           
  for(var i=0;i<count;i++)   
  {   
     if($("#cusChildTypeId").get(0).options[i].value == value)   
     {   
       $("#cusChildTypeId").get(0).remove(i);   
        break;   
     }
  }
Copy after login

10. Clear Select:
1>. $("#cusChildTypeId").empty ();
2>. $("#cusChildTypeId").get(0).options.length = 0;

Example:

$("document").ready(function(){
$("#btn1").click(function(){
$("[name='checkbox']").attr("checked",'true');//全选
})
$("#btn2").click(function(){
$("[name='checkbox']").removeAttr("checked");//取消全选
})
$("#btn3").click(function(){
$("[name='checkbox']:even").attr("checked",'true');//选中所有奇数
})
$("#btn4").click(function(){
$("[name='checkbox']").each(function(){//反选
if($(this).attr("checked")){
$(this).removeAttr("checked");
}
else{
$(this).attr("checked",'true');
}
})
})
$("#btn5").click(function(){//输出选中的值
var str="";
$("[name='checkbox'][checked]").each(function(){
str+=$(this).val()+"\r\n";
//alert($(this).val());
})
alert(str);
})
})
Copy after login

The above is the detailed content of Summary of common methods for setting and obtaining Select. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel.

How to set the scheduled time for publishing works on Douyin? How does it set the video duration? How to set the scheduled time for publishing works on Douyin? How does it set the video duration? Mar 27, 2024 pm 06:11 PM

How to set the scheduled time for publishing works on Douyin? How does it set the video duration?

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts)

Where is the Douyin tag set? How can it be tagged so that it can be pushed accurately? Where is the Douyin tag set? How can it be tagged so that it can be pushed accurately? Mar 27, 2024 am 11:01 AM

Where is the Douyin tag set? How can it be tagged so that it can be pushed accurately?

Where to get Google security code Where to get Google security code Mar 30, 2024 am 11:11 AM

Where to get Google security code

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

How to set font size on mobile phone (easily adjust font size on mobile phone)

How to set the watermark in the middle on Weibo_How to set the watermark in the middle on Weibo How to set the watermark in the middle on Weibo_How to set the watermark in the middle on Weibo Mar 29, 2024 pm 03:31 PM

How to set the watermark in the middle on Weibo_How to set the watermark in the middle on Weibo

See all articles