Select "select" element with jquery

3

I created on my page a select multiple and I need to get its values in PHP as vector, so I added [] to the end of the name. Example:

<select id="selecionados[]" name="selecionados[]" size="20" multiple ">
 <option value="A">A</option>
 <option value="B">B</option>
</select>

So long, however, I need to get the selecionados[] element with Jquery before submitting the form. However, neither with $('#selecionados') nor $('#selecionados[]') works.

How can I get this element by jQuery?

    
asked by anonymous 07.07.2014 / 20:20

3 answers

3
The id of select does not have to contain [] , change to id="selecionados"

<select id="selecionados" name="selecionados[]" size="20" multiple >
 <option value="A">A</option>
 <option value="B">B</option>
</select>

And in jQuery, for example:

$('#selecionados').on('change', function(){
    alert($(this).val());
});

Example: JSFiddle

    
07.07.2014 / 20:26
3

I think the question here is how to use [] inside the jQuery selector ...

I have already given a answer about it , and in your case you can use this:

Using \ or in case of using name , with quotation marks inside. Example:

var selectID = $('#selecionados\[\]');
console.log(selectID); // selecionando por ID

var selectNome = $('select[name=selecionados\[\]]');
console.log(selectID); // selecionando por nome/name

link

    
07.07.2014 / 20:41
2

You can use the following:

$("select[id^=selecionados]");

This will bring the element you described.

    
07.07.2014 / 20:28