Jquery get select value

I'm having trouble trying to get the value of a select box using jquery

<select id='form[1]' name='form[1]'>
    <option val='1'>1</option>
    <option val='2'>2</option>
</select>

I tried:

$('#form[1]').val()

and

$('#form\\[1\\]').val()

But no luck

It ended up working for me, I would post it as an answer, but I can't for 8 hours

var num = $(".forms").attr('name', 'form[1]');

$(num[0]).val();

thank you for your help

+3
source share
3 answers

Give it a try $('#form\\[1\\]').val(). You need to avoid[]

+6
source

Your problem is with the selector. Brackets must be shielded:$('#form\\[1\\]').val()

As an aside, the convention I've seen from many frameworks is to make id something form-1

+3
source

, . , , [].

$('select[id="form[1]"]')
+1

All Articles