1

On button click i want to fetch selected value of multiple select boxes. i can able to fetch value from array of textboxes but im not able to get select box value. it returns empty array.

$("#add_user_button").click(function(){
		

	var username=[];	   
	$("input[name=username]").each(function(){
	 username.push($(this).val());
	});

	var usertype=[];
	$("input[name=usertype]").each(function(){
		usertype.push($(this).val());
	}); 
	
  var ajaxdata={"UserName":username,"UserType":usertype};	
  
			console.log(JSON.stringify(ajaxdata));
  
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<div id="user_group">
        <input type="text" id="user_username" placeholder="User Name" name="username">
           <select id="user_usertype"  name="usertype">
                   <option value="1">Admin</option>
                   <option value="2">Normal</option>
              </select>
</div>

<div class="clone_user_input"><div id="user_group_1">
         <div class="form-group">
                <input type="text" id="user_username" placeholder="User Name" name="username">
                 <select id="user_usertype"  name="usertype">
                   <option value="1">Admin</option>
                   <option value="2">Normal</option>
              </select>
     </div>                                                              
 </div>
  <button  id="add_user_button">Add User</button>

1 Answer 1

2

Select is not input. Use:

var usertype=[];
$("select[name=usertype]").each(function(){
    usertype.push($(this).val());
}); 

Also note that you have duplicate IDs for elements. IDs should be always unique. You can rather use same class names as an alternative to this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.