简体   繁体   中英

jQuery: If user changes select to specific option then change different input value

I have a select box like so:

<select id="update_type_picker" name="update_type_picker">
 <option value="play">Played</option>
 <option value="play">playing</option>
 <option value="want">Want</option>
 <option value="rating">Rate</option>
</select>

And an input like this:

<input id="playing" name="playing" type="hidden">

And I'm trying to make this jquery work:

   $(document).ready(function () {     
    $("select#update_type_picker").change( function() {
        var text = this.text;
        if (text = "playing") {
            $("input#playing").attr('value', '1');
        } else {
            $("input#playing").attr('value', '');
        }
    });
   });

I need to use text (not value) because two of the values are the same. With the jquery above the input value changes to 1 regardless of which option I choose. How can I make this work they way I need it to? Thanks!

You have to use:

var text = $(this).find("option:selected").text();

And as mentioned in a comment, you have to use == or === to compare strings in the if , not = .

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