JQuery - 获取选择值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6706803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery - Get select value
提问by Stephen
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:
我正在尝试通过 jQuery 从下拉列表中获取选定的值。我有一些 javascript 在单击发送时验证表单,以确保没有空格,代码如下:
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 value I want. As you can see I've tried:
在这种情况下,国籍是我想要的值。正如你所看到的,我已经尝试过:
var nationality = $("#dancerCountry").val();
which doesn't seem to work.
这似乎不起作用。
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.
如果我放置以下警报语句: alert(obj.options[obj.selectedIndex].text); 在 case“select-one”之后输出正确的值,所以我知道它被正确传递。
I'm just not sure how to capture it in the sendDetailsDancer function.
我只是不确定如何在 sendDetailsDancer 函数中捕获它。
Any help appreciated.
任何帮助表示赞赏。
Regards, Stephen
问候, 斯蒂芬
回答by BenM
var nationality = $("#dancerCountry").val();
should work. Are you sure that the element selector is working properly? Perhaps you should try:
var nationality = $("#dancerCountry").val();
应该管用。你确定元素选择器工作正常吗?也许你应该尝试:
var nationality = $('select[name="dancerCountry"]').val();
回答by Frédéric Hamidi
val()returns the valueof the <select>
element, i.e. the value
attribute of the selected <option>
element.
VAL()返回值的的<select>
元件,即,在value
所选择的属性<option>
的元素。
Since you actually want the inner textof the selected <option>
element, you should match that element and use text()instead:
由于您实际上想要所选元素的内部文本,因此<option>
您应该匹配该元素并使用text()代替:
var nationality = $("#dancerCountry option:selected").text();