简体   繁体   中英

Jquery how to select option with value

I have a variable defined in my code as follows:

var select = $('.selMake');

and I want to select the option that has the value "placeholder". How do I select the option using the variable? I need to change the html of the option that has the value "placeholder"

Try

$("select.selMake").val("placeholder"); 

Using the variable:

var $select = $('.selMake'); 
$select.val("placeholder"); 

It's usually good practice to use the $ prefix when indicating a jquery object.

Edit based on comment: Try

$select.find('option[value="placeholder"]').text("newText"); 

What do you mean by "select" and "options"? is it dropdown select HTML?

If so, you can try using code below to select the option that have value of placeholder:

var select = $('.selMake');

//select the option that have value of placeholder
var optionWithPlaceholder = select.find("option[value='placeholder']");

//you can do anyting here
optionWithPlaceholder.html("after edit");

I tried that code here: http://jsfiddle.net/Lg67qcj1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM