js to get the number of elements with the same name and get the value of any one of them (get the last element value)

Reprinted from: http://blog.csdn.net/superit401/article/details/72629519

[javascript]  view plain copy  
  1. <input name="a" id="1" value="南海">  
  2.  <input name="a" id="2" value="特朗普">  
  3.  <input name="a" id="3" value="月光宝盒">  
  4.  <input name="a" id="4" value="大话西游">  
  5.   
  6. $(function(){  
  7.          // get the number of elements  
  8.     alert($('input:[name="a"]').length);   //4  
  9.         //Get the new way of writing the value of the first element  
  10.        alert($('input:[name="a"]:first').val()); //南海  
  11.         //Get the new way of getting the last element value  
  12.        alert($( 'input:[name="a"]:last' ).val()); // Westward Journey     
  13.         //Get the second element value writing  
  14.       alert($( 'input:[name="a"]:eq(1)' ).val()); //Trump  
  15. })  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326502897&siteId=291194637