javascript - jQuery将数组中的值放入文本框中但没有逗号
问题描述
我有一个数组["A", "B", "C"]
,我想在文本框中显示数组的元素,但没有逗号。它们将出现在文本框中,如下所示:ABC
function addtoFormula(){
var a = ["A", "B", "C"];
$.each([a], function( index, value ) {
var putvalue = value;
$("#formula").val(putvalue);
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="text" name="formula" id="formula">
<button onclick="addtoFormula();">Add</button>
解决方案
您根本不需要遍历数组。只需使用Array.prototype.join()
空字符串 ( ''
) 作为分隔符:
function addtoFormula(){
var a = ["A", "B", "C"];
$("#formula").val(a.join(''));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="text" name="formula" id="formula">
<button onclick="addtoFormula();">Add</button>