문제

입력 필드에 텍스트를 추가해야합니다 ...

도움이 되었습니까?

해결책

    $('#input-field-id').val($('#input-field-id').val() + 'more text');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="input-field-id" />

다른 팁

두 가지 옵션이 있습니다. Ayman의 접근 방식이 가장 간단하지만 추가 메모를 추가 할 것입니다. jQuery 선택을 실제로 캐시해야합니다. $("#input-field-id") 두 배:

var input = $( "#input-field-id" );
input.val( input.val() + "more text" );

다른 옵션, .val() 또한 인수로 기능을 할 수 있습니다. 이것은 여러 입력에서 쉽게 작업하는 장점이 있습니다.

$( "input" ).val( function( index, val ) {
    return val + "more text";
});

한 번 더 추가하는 것을 사용하려는 경우 기능을 작성할 수 있습니다.

//Append text to input element
function jQ_append(id_of_input, text){
    var input_id = '#'+id_of_input;
    $(input_id).val($(input_id).val() + text);
}

그냥 전화 할 수있는 후에 :

jQ_append('my_input_id', 'add this text');

당신은 아마 찾고있을 것입니다 val ()

	// Define appendVal by extending JQuery
	$.fn.appendVal = function( TextToAppend ) {
		return $(this).val(
			$(this).val() + TextToAppend
		);
	};
//_____________________________________________

	// And that's how to use it:
	$('#SomeID')
		.appendVal( 'This text was just added' )
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<textarea 
          id    =  "SomeID"
          value =  "ValueText"
          type  =  "text"
>Current NodeText
</textarea>
  </form>

이 예제를 만들 때 나는 어떻게 든 조금 혼란스러워졌습니다. "Valuetext"vs>현재 nodetext<그렇지 않습니다 .val() 데이터의 데이터를 실행해야합니다 기인하다? 어쨌든 나와 당신은 조만간 이것을 정리할 수 있습니다.

그러나 지금의 요점은 다음과 같습니다.

작업 할 때 양식 데이터 사용 .val ().

대부분을 다룰 때 데이터 만 읽으십시오 태그 사용 사이 .텍스트() 또는 .Append () 텍스트를 추가합니다.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <style type="text/css">
        *{
            font-family: arial;
            font-size: 15px;
        }
    </style>
</head>
<body>
    <button id="more">More</button><br/><br/>
    <div>
        User Name : <input type="text" class="users"/><br/><br/>
    </div>
    <button id="btn_data">Send Data</button>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $('#more').on('click',function(x){
                var textMore = "User Name : <input type='text' class='users'/><br/><br/>";
                $("div").append(textMore);
            });

            $('#btn_data').on('click',function(x){
                var users=$(".users");
                $(users).each(function(i, e) {
                    console.log($(e).val());
                });
            })
        });
    </script>
</body>
</html>

산출enter image description here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top