2014-06-08 4 views
1

페이지로드시 드롭 다운 선택 요소 (양식 내)의 값을 가져오고 변경시 다른 값 (숨김)으로 전달하려고합니다. 지금까지 시도한 코드는 다음과 같습니다.변경시 문서에서 선택 값 가져 오기

이것은 하나의 페이지로드에서 작동하지만, 변경할 때 현재 작업 :

 jQuery(document).ready(function() { 
     var comment_post_ID = jQuery("#alldentists").val(); 
      jQuery("#comment_post_ID").val(comment_post_ID); 
     }); 

이이 옵션을 선택 변경에 pageload하지만 작업에서 작동하지 않습니다.

 jQuery(document).ready(function() { 

      jQuery("#alldentists").change(function() { 
       var comment_post_ID = jQuery("#alldentists").val(); 

      }); 
      jQuery("#comment_post_ID").val(comment_post_ID); 
     }); 

pageload와 변경시 모두 값을 얻을 수있는 방법이 있습니까? jQuery 1.11.0을 사용하고 있습니다.

+0

만'.change() '함수 내에서 사용할 수 있도록 변수의 범위를 제한한다. 빈 값으로 외부에 선언 해보십시오. –

답변

1

변수 "comment_post_ID"을 전역으로 선언하십시오.

<select name="alldentists" id="alldentists"><option>One</option> 
    <option>Two</option> 
     <option>Three</option> 
    </select> 
    <input type="text" id="comment_post_ID"/> 

jQuery(document).ready(function() { 
     var comment_post_ID=jQuery("#alldentists").val(); 
     jQuery("#alldentists").on('change', function(e) { 
     comment_post_ID = jQuery("#alldentists").val(); 
     jQuery("#comment_post_ID").val(comment_post_ID); 
     }); 
     jQuery("#comment_post_ID").val(comment_post_ID); 
}); 

데모 :이`var에 comment_post_ID` 같은데요

http://jsfiddle.net/3PPvH/1/

+0

고마워, 그게 내가 필요한거야. 너는 구세주 야. – Abhik

+0

@Abhik, 안녕하세요. 기꺼이 도와 줘. – RGS

관련 문제