반응형
JQuery-선택 값 가져 오기
jQuery를 통해 드롭 다운 목록에서 선택한 값을 가져 오려고합니다. 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;
}
이 경우 국적은 내가 원하는 값입니다. 보시다시피 나는 시도했습니다.
var nationality = $("#dancerCountry").val();
작동하지 않는 것 같습니다.
다음 경고문을 넣으면 : alert (obj.options [obj.selectedIndex] .text); case "select-one"후 올바른 값이 출력되므로 올바르게 전달되었음을 알 수 있습니다.
sendDetailsDancer 함수에서 캡처하는 방법을 잘 모르겠습니다.
도움을 주시면 감사하겠습니다.
감사합니다, Stephen
var nationality = $("#dancerCountry").val();
작동해야합니다. 요소 선택기가 제대로 작동하고 있습니까? 아마도 다음을 시도해야합니다.
var nationality = $('select[name="dancerCountry"]').val();
val() returns the value of the <select>
element, i.e. the value
attribute of the selected <option>
element.
Since you actually want the inner text of the selected <option>
element, you should match that element and use text() instead:
var nationality = $("#dancerCountry option:selected").text();
참고URL : https://stackoverflow.com/questions/6706803/jquery-get-select-value
반응형
'UFO ET IT' 카테고리의 다른 글
gem install을 사용하여 이전 버전의 나침반으로 어떻게 다운 그레이드합니까? (0) | 2020.11.29 |
---|---|
Python strip () 여러 문자? (0) | 2020.11.29 |
Mac 명령 줄에서 mysql에 액세스 할 수 없습니다. (0) | 2020.11.29 |
좋은 형식을 출력하는 방법 PHP XML (0) | 2020.11.29 |
Python unittest 전달 인수 (0) | 2020.11.29 |