2016-06-16 3 views
0

클릭시 자바 스크립트 기능을 실행하는 버튼이 내 페이지에 있습니다. 이제는 사용자가 입력 텍스트 상자에 값을 입력해야하는 함수에 변수가 있습니다. 여기 내 코드가있다.버튼 클릭시 JavaScript 변수 값 설정

<input type="text"> </input> 
    <button id ="buttton" class="button" onclick="myfunction()">Create Envelope</button> 

    <script> 
    function myfunction() { 
    var sub =""; // I am thinking of writing jquery 
} 
    </script> 

사용자가 버튼을 클릭 할 때까지 my variable sub는 사용자가 입력 필드에 입력 한 값을 보유해야합니다.

답변

3

사용 InputElement (자바 스크립트)의 .value 속성 또는 jQuery

$(ELEMENT).val() 방법은 요소를 선택 요소에 독특한 ID을 제공합니다. 요소를 선택할 수있는 다른 방법은 class-selector 또는 attribute-selector 등입니다.

function myfunction() { 
 
    var sub = $('#input').val(); 
 
    console.log(sub); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<input type="text" id='input'> 
 
<button id="buttton" class="button" onclick="myfunction()">Create Envelope</button>

관련 문제