Note that there are some explanatory texts on larger screens.

plurals
  1. POJQuery - Get select value
    text
    copied!<p>I'm trying to get the selected value from a drop down list via jQuery. I have a bit of javascript that validates a form when I click SEND, to make sure there are no blanks, code is as follows:</p> <pre><code>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 &lt; 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 &lt; 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('&lt;center&gt;&lt;img src="loading.gif" width="32" height="32" /&gt;&lt;/center&gt;'); $("#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; } </code></pre> <p>In this case Nationality is the value I want. As you can see I've tried:</p> <pre><code>var nationality = $("#dancerCountry").val(); </code></pre> <p>which doesn't seem to work. </p> <p>If I put the following alert statement: alert(obj.options[obj.selectedIndex].text); after case "select-one" the correct value is output so I know its being passed correctly. </p> <p>I'm just not sure how to capture it in the sendDetailsDancer function.</p> <p>Any help appreciated.</p> <p>Regards, Stephen</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload