[jquery] 입력 필드에 텍스트 추가

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



답변

    $('#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> Current NodeText < value 속성 .val()의 데이터에서 실행 되지 않아야 합니까? 어쨌든 나와 당신은 조만간 이것을 정리할 수 있습니다.

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

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

태그 사이에 있는 대부분의 읽기 전용 데이터 를 처리 할 때 .text () 또는 .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>

산출
여기에 이미지 설명 입력


답변