问题描述
我正在尝试通过 jQuery 从下拉列表中获取选定的值.我有一些 javascript 在我点击发送时验证表单,以确保没有空格,代码如下:
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:
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:
";
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] + "
";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "
";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "
";
}
break;
case "checkbox":
if (obj.checked == false){
alertMsg += " - " + fieldDescription[i] + "
";
}
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] + "
";
}
}
}
}
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 value I want. As you can see I've tried:
var nationality = $("#dancerCountry").val();
这似乎不起作用.
如果我把下面的警告语句: alert(obj.options[obj.selectedIndex].text);在 caseselect-one"之后输出正确的值,所以我知道它被正确传递.
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.
我只是不确定如何在 sendDetailsDancer 函数中捕获它.
I'm just not sure how to capture it in the sendDetailsDancer function.
感谢任何帮助.
问候,斯蒂芬
推荐答案
var nationality = $("#dancerCountry").val();
应该可以工作.你确定元素选择器工作正常吗?也许你应该试试:
var nationality = $("#dancerCountry").val();
should work. Are you sure that the element selector is working properly? Perhaps you should try:
var nationality = $('select[name="dancerCountry"]').val();
这篇关于JQuery - 获取选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!