Aym*_*ieh 197
$('#input-field-id').val($('#input-field-id').val() + 'more text');
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="input-field-id" />
Run Code Online (Sandbox Code Playgroud)
gna*_*arf 108
有两种选择.艾曼的方法是最简单的,但我会添加一个额外的注释.你应该真的缓存jQuery选择,没有理由调用$("#input-field-id")
两次:
var input = $( "#input-field-id" );
input.val( input.val() + "more text" );
Run Code Online (Sandbox Code Playgroud)
另一个选项,.val()
也可以将函数作为参数.这有利于轻松处理多个输入:
$( "input" ).val( function( index, val ) {
return val + "more text";
});
Run Code Online (Sandbox Code Playgroud)
Mar*_*gus 17
如果您计划使用多次追加,则可能需要编写一个函数:
//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);
}
Run Code Online (Sandbox Code Playgroud)
你可以打电话之后:
jQ_append('my_input_id', 'add this text');
Run Code Online (Sandbox Code Playgroud)
// 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' )
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
好吧,在创建这个例子时,我不知何故有点困惑。" ValueText " vs > Current NodeText < 不.val()
应该在value属性的数据上运行吗?不管怎样,我和你我迟早会解决这个问题。
然而,现在的重点是:
处理表单数据时使用.val()。
在处理标签之间的大部分只读数据时,使用.text()或.append()来附加文本。
归档时间: |
|
查看次数: |
245069 次 |
最近记录: |