我需要一些文本附加到输入字段...

有帮助吗?

解决方案

    $('#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" />

其他提示

有两个选项。艾曼的方法是最简单的,但我想补充一个额外的注吧。你真的应该缓存jQuery的选择,没有理由叫$("#input-field-id")两次:

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

另一种选择, .val() 也可以采取函数作为参数。这具有的工作容易地在多个输入的advantange:

$( "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 ” 与> 当前NodeText <是不是.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>

输出 “在这里输入的图像描述”

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top