Select "select" element with jquery

Asked

Viewed 1,360 times

3

I created on my page one select multiple and I need to take your values in PHP in vector form, so I added [] at 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 far so good, but I need to send the form before sending the element selecionados[] with Jquery. But not with $('#selecionados') nor $('#selecionados[]') work.

How can I get this element by jQuery?

3 answers

3


The id of select need not contain the [], 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, example:

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

Example: Jsfiddle

3

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

I already gave a answer about that, and in your case can use so:

Using \\ or if using the name, with quotes inside. Example:

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

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

http://jsfiddle.net/VCZ3d/

2

You can use the following:

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

This will bring the element you described.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.