JQuery - get selected value

I am trying to get the selected value from a dropdown through jQuery. I have a little javascript that validates the form, when I press SEND to make sure there are no spaces, the code looks like this:

function formCheckDancer(formobj){ // Enter name of mandatory fields var fieldRequired = Array("dancerStageName", "dancerFullName", "dancerPhone", "dancerEmail", "dancerCountry", "dancerAvailableDate"); // Enter field description to appear in the dialog box var fieldDescription = Array("Stage Name", "Full Name", "Phone Number", "Email Address", "Nationality", "Availability"); // dialog message var alertMsg = "Please complete the following fields:\n"; var l_Msg = alertMsg.length; for (var i = 0; i < fieldRequired.length; i++){ var obj = formobj.elements[fieldRequired[i]]; if (obj){ switch(obj.type){ case "select-one": if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].text == "..."){ alertMsg += " - " + fieldDescription[i] + "\n"; } break; case "select-multiple": if (obj.selectedIndex == -1){ alertMsg += " - " + fieldDescription[i] + "\n"; } break; case "text": case "textarea": if (obj.value == "" || obj.value == null){ alertMsg += " - " + fieldDescription[i] + "\n"; } break; case "checkbox": if (obj.checked == false){ alertMsg += " - " + fieldDescription[i] + "\n"; } break; default: } if (obj.type == undefined){ var blnchecked = false; for (var j = 0; j < obj.length; j++){ if (obj[j].checked){ blnchecked = true; } } if (!blnchecked){ alertMsg += " - " + fieldDescription[i] + "\n"; } } } } if (alertMsg.length == l_Msg){ return sendDetailsDancer(); //Send email if all field are populated. return true; }else{ alert(alertMsg); return false; } } function sendDetailsDancer(){ var stagename = $("input[name=dancerStageName]").val(); var fullname = $("input[name=dancerFullName]").val(); var phone = $("input[name=dancerPhone]").val(); var email = $("input[name=dancerEmail]").val(); var nationality = $("#dancerCountry").val(); var availability = $("input[name=dancerAvailableDate]").val(); $("#contact_form_result_dancer").html('<center><img src="loading.gif" width="32" height="32" /></center>'); $("#contact_form_result_dancer").show(); $.post("http://localhost/lapello/wp-content/themes/lapello/sendMailDancer.php", {stagename: stagename, fullname: fullname, phone: phone, email: email, nationality: nationality, availability: availability}, function (data){ $("#contact_form_result_dancer").html(data); }); setTimeout("contactReturnDancer()", 4000); return false; } 

In this case, Nationality is the meaning that I want. As you can see, I tried:

 var nationality = $("#dancerCountry").val(); 

which does not seem to work.

If I put the following warning statement: alert (obj.options [obj.selectedIndex] .text); after the case of "select-one" the correct value is displayed, so I know that it is transmitted correctly.

I'm just not sure how to capture it in the sendDetailsDancer function.

Any help was appreciated.

Regards, Stephen

+60
jquery select
Jul 15 '11 at 12:15
source share
2 answers

var nationality = $("#dancerCountry").val(); must work. Are you sure the element selector is working correctly? Maybe you should try:

 var nationality = $('select[name="dancerCountry"]').val(); 
+105
Jul 15 '11 at 12:20
source share

val () returns the value of the <select> element, that is, the value attribute of the selected <option> element.

Since you really need the inner text of the selected <option> element, you must map this element to text () instead:

 var nationality = $("#dancerCountry option:selected").text(); 
+48
Jul 15 '11 at 12:20
source share



All Articles