jQuery-获取选择值

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (72)

我正试图通过jQuery从下拉列表中获取选定的值。我有一些javascript,当我单击Send时验证表单,以确保没有空白,代码如下:

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;
        }

在这种例子下,Nationality是我想要的值。正如你所看到的,我试过:

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

这似乎不管用。

如果我放置以下警报语句:警报(obj.Options)obj.selectedIndex在大小写“SELECT-One”之后,输出正确的值,这样我就知道它被正确地传递了。

我只是不知道如何在sendDetailsDancer函数中捕获它。

提问于
用户回答回答于

var nationality = $("#dancerCountry").val();应该管用。你确定元素选择器工作正常吗?也许你应该试试:

var nationality = $('select[name="dancerCountry"]').val();
用户回答回答于

VAL()返回的的<select>元件,即,value所选择的属性<option>的元素。

既然你实际上需要所选元素的内部文本<option>,你应该匹配那个元素并使用text()来代替:

var nationality = $("#dancerCountry option:selected").text();

扫码关注云+社区

领取腾讯云代金券