William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

jQuery select selector

jQuery select selector

Here I list get/set jQuery <Select> selector’s Text and Value:

  //add 'onChange' event for Select
  //get select's Text
  var checkText=$("#select_id").find("option:selected").text();
  //get select'sValue
  var checkValue=$("#select_id").val(); 
  //get select's index
  var checkIndex=$("#select_id ").get(0).selectedIndex;
  //get select's max index
  var maxIndex=$("#select_id option:last").attr("index"); 
  $("#select_id ").get(0).selectedIndex=1;
  $("#select_id ").val(4); 
  $("#select_id option[text='jQuery']").attr("selected", true); 
  //append a Option
  $("#select_id").append("<option value='Value'>Text</option>");
  //unshift a Option
  $("#select_id").prepend("<option value='0'>Select</option>"); 
  //push a option
  $("#select_id option:last").remove(); 
  //shift a option
  $("#select_id option[index='0']").remove(); 
  //remove a option (val()=3)
  $("#select_id option[value='3']").remove();
  //remove a option (text()='4')
  $("#select_id option[text='4']").remove();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: