0

I'm stumped. I have a dropdown menu where a user selects an item.

<select name="rep-name" type="text" id="rep-name" size="" value="" >
    <option value></option>
    <option value="alex">alex</option>
    <option value="ben">ben</option>
    ...
</select>

The value is then retrieved...

$('#rep-name').val()

and sent to a database.

Usually it works fine but in some cases, it sends the value 'Array' to the database. Interestingly, in those cases, the serialize function on the form still gets the correct value of the item. So in other words:

$('#run-pma-form').serialize() // works fine
$('#rep-name').val() // fails

It works fine in ~95% of cases and unfortunately, I don't have info on what browsers are being used, etc when it incorrectly returns 'array.' I'm just wondering if anyone has run into this issue or has any clue why it might be happening.

4
  • 1
    Quick comment: remove type, size and value from the <select> Commented May 14, 2014 at 20:18
  • Good point. Could the multiple "value" attribute inside the select tag have caused this issue? Commented May 14, 2014 at 20:20
  • Possibly, you'll know for sure when you remove. But it sounds as if the server-side language was interpreting the value as an array. Commented May 14, 2014 at 20:21
  • @BenDavidow Yes, if it's <select multiple> then the value is an array of all the selected options. The server script has to loop over the values. Commented May 14, 2014 at 20:34

1 Answer 1

2

$("#rep-name")[n].val() will get you the value of any given option, but it's not correct to think of a select menu as having a value—what you want is the value of the currently selected option.

http://api.jquery.com/selected-selector/

$("#rep-name option:selected").val() should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.