jQuery-将值附加到输入,使其保持逗号分隔的列表
我的输入如下:jQuery-将值附加到输入,使其保持逗号分隔的列表,jquery,Jquery,我的输入如下: <input type="hidden" id="attachment-uuids" value=""> 结果: <input type="hidden" id="attachment-uuids" value="55555"> <input type="hidden" id="attachment-uuids" value="66666"> 结果: <input type="hidden" id="attachment-uuids
<input type="hidden" id="attachment-uuids" value="">
结果:
<input type="hidden" id="attachment-uuids" value="55555">
<input type="hidden" id="attachment-uuids" value="66666">
结果:
<input type="hidden" id="attachment-uuids" value="55555">
<input type="hidden" id="attachment-uuids" value="66666">
在这里,我想要以下内容:
<input type="hidden" id="attachment-uuids" value="55555, 66666">
当值为空时,以及当值不为空时,如何使用逗号分隔的列表追加值
谢谢在向字段添加值时只需添加一个条件
var cur_val = $('#attachment-uuids').val();
if(cur_val)
$('#attachment-uuids').val(cur_val + "," + new_val);
else
$('#attachment-uuids').val(new_val);
EDIT:如上所述,我正在条件运算符中对
val
进行不必要的求反。它可以在没有的情况下重写代码>作为:
(val ? ', ' : '')
我相信.append()
函数正是为了这个目的。我刚才试过:
$("#attachment-uuids").append(new_val);
将值附加到字段。这是可行的,“附件UUID”根据需要填充CSV。Append对输入不起作用。
但您可以使用:
$("#txt1").get(0).value+="+";
如果该值已经在列表中,该怎么办?@Nick问得好,这永远不会发生,但如果是这样,我可以在后端处理它。这是一种美丽的想法。谢谢如果我们要谈论优雅,那么最好翻转三元结构,去掉否定:P@mkoryak,我不知道你在说什么:)@mkoryak-说得好<代码>(val?,:“”)
更有意义。如果要逐个删除这些值,该怎么办?
$("#txt1").get(0).value+="+";