我如何使用jQuery将某些内容附加到表单元素的值,而不是更改它? [英] How can I use jQuery to append something to the value of a form element, as opposed to changing it?

查看:71
本文介绍了我如何使用jQuery将某些内容附加到表单元素的值,而不是更改它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在基本JS中做到这一点:

I know how to do this in basic JS:

document.getElementById("someTextarea").value += "stuff";

请注意+-我要添加值,而不是完全更改它.我将如何使用jQuery的 val() 做到这一点?

Note the + - I'm adding to the value, not completely changing it. How would I do that using jQuery's val()?

推荐答案

$('#foo').val($('#foo').val() + 'something');

(当然,您可以并且应该缓存该元素).

(Of course you can and should cache the element).

另一种方式(非常jQuery风格):

Another way (very jQuery style):

$('#foo').val(function(){
    return this.value + "something";
});

这篇关于我如何使用jQuery将某些内容附加到表单元素的值,而不是更改它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆