jQuery 如何使用jQuery将文本附加到文本框值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10293001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:05:47  来源:igfitidea点击:

How to append text to text box value using jQuery?

jqueryvalidation

提问by Mangala Edirisinghe

I need append - to the present text box value without replacing present text. I tried with this code.

我需要将 - 附加到当前文本框值而不替换当前文本。我试过这个代码。

if(len.length==4){
     $("-").appendTo("#date").val();
}

but it failed.

但它失败了。

回答by gdoron is supporting Monica

Though you got several(but not all) correct answers, I want to show another way to do it:

尽管您得到了几个(但不是全部)正确答案,但我想展示另一种方法:

$('#date').val(function(index, value) {
    return value + '-';
});?

.val( function(index, value) )

.val( function(index, value) )

function(index, value)A function returning the value to set. this is the current element. Receives the index position of the element in the set and the old value as arguments.

function(index, value) 返回要设置的值的函数。这是当前元素。接收集合中元素的索引位置和旧值作为参数。

source

来源

If you don't want to use functionfor doing it, use this:

如果您不想使用function它,请使用以下命令:

var $date = $('#date');
$date.val($date.val() + '-');

回答by Richard Neil Ilagan

You've got to retrieve the current value, and append to that.

您必须检索当前值,并将其附加到该.

var $date = $('#date');
$date.val($date.val() + '-');

回答by Florian Margaine

The most elegant way I've found includes not using jQuery so much.

我发现的最优雅的方式包括不使用 jQuery。

if ( len.length === 4 ) {
    var date = $( '#date' )[0] // faster to write than "document.getElementById( 'date' )"
    date.value += '-'
}

Fiddle: http://jsfiddle.net/Ralt/T9mUT/3/

小提琴:http: //jsfiddle.net/Ralt/T9mUT/3/

回答by Warren Sergent

jQuery('#date').val(jQuery('#date').val() + '-');

回答by KoolKabin

try:

尝试:

$('#date').val($('#date').val() + '-');

回答by Hugh Seagraves

I was just doing this. I used .append(myValue); here I'm looping through an Ajax result set:

我就是这样做的。我使用了 .append(myValue); 在这里,我正在遍历 Ajax 结果集:

        $.each(result, function (i, obj) {
            $.each(obj, function (i, item) {
                $('#txtValueList').append(item);
            })
        })