admin管理员组

文章数量:1327843

HTML

<select id="myDDL">
    <option selected="selected" value="0">default</option>
    <option value="1">apples</option>
    <option value="2">oranges</option>
</select>

Javascript

setFieldValue("myDDL", "apples");

function setFieldValue(field, value)
{
    var val = $("#" + field + " option[text]");
    console.log(val);
    $("#" + field).val(val);
}

JSFiddle

/

I would like to select an option within a select field based on its text, i.e.:

var val = $("#" + field + " option[text='" + value + "']");

However as soon as I add [text] after option the selector seems to select the entire document, whereas $("#" + field + " option"); simply selects all option tags within the select field, as I would expect.

Could you please explain why it does not recognise "[text]" and how I can correct this?

Thanks

HTML

<select id="myDDL">
    <option selected="selected" value="0">default</option>
    <option value="1">apples</option>
    <option value="2">oranges</option>
</select>

Javascript

setFieldValue("myDDL", "apples");

function setFieldValue(field, value)
{
    var val = $("#" + field + " option[text]");
    console.log(val);
    $("#" + field).val(val);
}

JSFiddle

https://jsfiddle/nnvh7e43/

I would like to select an option within a select field based on its text, i.e.:

var val = $("#" + field + " option[text='" + value + "']");

However as soon as I add [text] after option the selector seems to select the entire document, whereas $("#" + field + " option"); simply selects all option tags within the select field, as I would expect.

Could you please explain why it does not recognise "[text]" and how I can correct this?

Thanks

Share Improve this question asked Sep 14, 2015 at 14:14 AndrewBAndrewB 8361 gold badge7 silver badges26 bronze badges 2
  • possible duplicate of jquery find element by text – Andrey Commented Sep 14, 2015 at 14:17
  • possible duplicate of jQuery - setting the selected value of a select control via its text description – PeterKA Commented Sep 14, 2015 at 14:52
Add a ment  | 

4 Answers 4

Reset to default 7

apples is text of option and not its value. You need to use .filter() function here:

 $("#" + field + " option").filter(function(){
       return $(this).text() === value ;
 });

Also, You are trying to get the option value by text and then set its value to select. You can rather set the selected property of option to true for setting it selected:

 $("#" + field + " option").filter(function(){
       return $(this).text() === value ;
 }).prop('selected', true);

Working Demo

The [] css selector can only be used on attributes (href, selected, value, class, id, ...). You can't use it on the content of an html element.

Milind Anantwar solution is the way to go. Use filter :

 $("#" + field + " option").filter(function(){
     return $(this).text() === value ;
 });

You can do this:

function setFieldValue(field, value) {

    // First filter all option by text, then get that option value
    var val = $("#" + field + " option").filter(function () {
        return $.trim($(this).text()) == value;
    }).val();
    console.log(val);

    // Set the option w/ text value here
    $("#" + field).val(val);
}

FIDDLE DEMO

Selectors like tagname[attributename] look for an actual attribute on the tagname element. Your option elements do not have a text attribute. So you need to either give them a text attribute or, probably preferable, modify your selector to examine the actual inner text of the element rather than look for an attribute, e.g. with contains:

var val = $("#" + field + " option:contains(" + value + ")");

本文标签: javascriptJquery selectortext equalsnot workingStack Overflow