2016-07-18 9 views
-1

에 jQuery를 사용하여 드롭 다운의 선택된 값을 돌려 I 인터페이스에서 두 값을 수신하는 인터페이스가 있습니다자바 스크립트 함수

하나는 날짜이고 다른 하나는 드롭 다운 메뉴에서 와서 문자열입니다.

날짜 값은 괜찮지 만 드롭 다운 메뉴의 값은 아닙니다.

항상 "기본"값을 선택합니다. 사용자가 선택하는 값을 전달하고 싶습니다. 그래서 대신 값을 선택하는 사용자의 선택은 항상 기본적으로 "사용자 선택"값입니다

Resources.Resources.SelectUser

@

을하고있다.

<div id="employee_info" > 
<div class="search_employee" id="search_employee"> 
     <select name="search_emp" id="search_emp" style="width: 174px"> 
      @{ 
       if (ViewBag.DataActiveEmployee == null || ((IEnumerable<MvcApplication1.Models.ActiveUsersList>)ViewBag.DataActiveEmployee).Count() == 0) 
       { 
        //@:<h3>No records were processed.</h3> 

       } 
       else 
       { 
       @: 
       <option>@Resources.Resources.SelectUser</option> 
        foreach (var usr in ViewBag.DataActiveEmployee) 
        {  
       <option id="selected_user">@usr.EmployeeName</option>   
        } 
       @: 
} 
      } 
     </select> 
</div> 
</div> 

<div id="datePicker"> 
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
</div> 

<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
<p>@*@Resources.Resources.Date: *@<input type="text" id="datepicker"></p> 
<script name="select_date" id="select_date"> 

    $(function() { 
     var userName = $('#search_emp').val(); 
     $("#datepicker").datepicker({ 
      //defaultDate: "+1w", 
      changeMonth: true, 
      changeYear: true, 
      numberOfMonths: 1, 
      minDate: "01/01/2008" 
     }); 
     $("button.action").click(function() { 
      //console.log(select_date); 
      var date = $('#datepicker').val().toString(); 
      $.ajax('EmployeeDate', { 
       data: { 
        strUserName: userName, 
        strDate: date 
       }, 
       success: function (data, textStatus, jqXHR) { 
        //this will happen on success of request 
        $('#DataUser').html(data); 
       }, 
       error: function() { 
        console.log("error handler when ajax request fails... "); 
       }, 

      }); 
     }); 
    }); 
</script> 
<button class="action" type="button" id="button_select">@Resources.Resources.ButtonFind</button> 


나는 또한이 tryied :

코드는 다음과

var userName = $('select#search_emp option:selected').val(); 

하지만 동일한 결과를 가지고있다.

아이디어가 있으십니까? 미리 감사드립니다!

답변

1
var userName = $('#search_emp').val(); 

페이지가로드 될 때 (document.ready 기능 내에서) 실행 중입니다. 그러면 그 순간에 선택된 값이 무엇이든간에 userName 변수에 저장됩니다. 버튼 클릭을 처리하게되면 드롭 다운에서 해당 값을 업데이트하지 않습니다. 해당 코드를 click 이벤트 핸들러 내부로 이동하면 이전 값이 아닌 해당 시점의 드롭 다운에서 선택한 값을 가져옵니다.