How does jquery get the selected value based on the multiple marquee name.

Source: Internet
Author: User
Tags pear

Using the Multi box name to get the selected value can be implemented with the following jquery code

$ ("input:checkbox[name= ' Test ']:checked"). each (function () {//Traversal name=test multi-box
$ (this). Val (); The value of each selected item
});

Example Demo: Give two sets of multi-box, click the button to get two groups of selected items

The sample code is as follows

Creating HTML Elements

<div class= "box" >
	<span> Enter your user name to limit the combination of letters, numbers, or underscores
	:</span><br> <div class= "content ">
		<span> fruit:</span><br>
		<input type=" checkbox "name=" fruit "value=" pear "/> Pear
		<input type= "checkbox" name= "fruit" value= "plum"/> Plum
		<input type= "checkbox" name= "fruit" value= "chestnut"/ > Chestnut
		<input type= "checkbox" name= "Fruit" value= "lychee"/> lychee <br>
		<span> vegetable:</span> <br>
		<input type= "checkbox" name= "vegetable" value= "greens"/> Greens <input
		type= "checkbox" Name= " Vegetable "value=" radish "/> Radish
		<input type=" checkbox "name=" vegetable "value=" potatoes "/> potatoes <input
		type= "checkbox" name= "vegetable" value= "eggplant"/> Eggplant
	</div>		
	<input type= "button" value= "Submit" >
</div>

Set CSS Styles

Div.box{width:300px;padding:20px;margin:20px;border:4px dashed #ccc;}
Div.box span{color: #999; font-style:italic;}
div.content{width:250px;margin:10px 0;padding:20px;border:2px solid #ff6666;}
input[type= ' checkbox ']{margin:5px;}
Input[type= ' button ']{height:30px;margin:10px;padding:5px 10px;}

Writing jquery Code

$ (function () {
	//Set property value
	$ ("Input:button"). Click (function () {
		var fruit = "";
		var vegetable = "";
		$ ("Input:checkbox[name= ' fruit ']:checked"). each (function () {
			fruit + = $ (this). Val () + "";
		});
		$ ("input:checkbox[name= ' vegetable ']:checked"). each (function () {
			vegetable + = $ (this). Val () + "";
		});
		Alert ("Selected fruit:" +fruit+ ", Selected vegetables:" +vegetable);})

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.