I have a problem like this:
There is Select:
& lt; select id = "make" name = "make" & gt; & lt; option value = "0" & gt; ---------------------- & lt; / option & gt; & lt; option value = "Mercedec" & gt; Mercedec & lt; / option & gt; & lt; option value = "Mazda" & gt; Mazda & lt; / option & gt; & lt; option value = "Audi" & gt; Audi & lt; / option & gt; & lt; / select & gt;
And I wrote js code to the console:
$ ("# make [value = 'Mazda']"). attr ("selected", "selected");
Returns the desired element option, selected = “selected”, but I’m looking at the list and the first option (value = 0) is still selected.
$ ("# make [value = 'Mazda']"). attr ("selected", "");
Answer 1, authority 100%
I would venture to suggest that this is some feature of the JS implementation in Chrome. Your code runs quite well under IE (10th version) and Firefox (23rd). But in Chrome (28th) the problems you described occur. I have not tested it in Opera.
One way or another, with the help of this miracle crutch:
var val = text = 'Mazda'; $ ("select option [value =" + val + "]"). attr ('selected', 'true'). text (text);
Answer 2, authority 70%
In current versions of jQuery, to change
select and select
checkbox , use the
.prop () method
$ ('select option [value = "Mazda"]'). prop ('selected', true);
option object and does not add attributes, that is, nothing will change in the markup. Although adding the attribute so far works with
option (checked in Opera), it doesn’t work with
Answer 3, authority 50%
$ ('# make'). val ('Mazda')
Answer 4, authority 20%
To change things like selected and checked, you need to use prop, not attr.
Answer 5, authority 10%
$ ("# make"). val ("Mazda"). change ()
I don’t understand the question, but it is possible
$ ("# make option [value = 'Mazda']"). val ('Mazda'). attr (" selected ", true);
I personally prefer the following one-liner method:
$ ("select"). val (""). trigger ("chosen: updated")
Should work in all browsers.
Accordingly, instead of an empty line, you need to use the text of the first
option ‘a, but as a rule, the default
option should be empty, and not contain 0, etc.
Accordingly, substituting the value of any
option ‘and you can select it.
selected attribute must be applied to the
$ ("# make & gt; option [value = 'Mazda']"). attr ("selected", "selected" );
$ ("# make"). val ("Mazda");