所以我遇到了这个问题。我正试图摆脱那些逗号。也使用了varArray.join(" ")
,但没有成功。我甚至创建了一个for
循环,但它没有用(好吧它可能有效,但我是初学者,虽然我对此表示怀疑)。
以下是代码:
let varArray = [];
let rufus1 = document.getElementById('rufus1');
let rufus2 = document.getElementById('rufus2'); // variables for easing syntax
function arrayGiver() {
varArray.push('<div>' + document.getElementById('rufus1').value + '</div>');
// I've used `<div>` instead of `<br>` for line breaking the elements, but when I'll be making bigger more complex projects I should probably use `<br>` shouldn't I?
rufus2.innerHTML = ("You have just added to your collection:" + "<br/>" +
varArray);
//this cleans the input after executing the function
rufus1.value = '';
}
//this makes the function execute on an enter key press
document.addEventListener('keydown', function(enterKey) {
if (enterKey.keyCode === 13) {
arrayGiver();
}
})
<input id="rufus1">
<br>
<br>
<button onclick="arrayGiver();">Bleh</button>
<p id="rufus2"></p>
答案 0 :(得分:0)
您可以使用innerHTML
或其他循环将数组元素添加到varArray.forEach(e => rufus2.innerHTML += e);
对于这个答案我用过:
e
迭代数组,将每个元素(rufus2.innerHTML
)添加到let varArray = [];
let rufus1 = document.getElementById('rufus1');
let rufus2 = document.getElementById('rufus2'); // variables for easing syntax
function arrayGiver() {
varArray.push('<div>' + document.getElementById('rufus1').value + '</div>');
// I've used `<div>` instead of `<br>` for line breaking the elements, but when I'll be making bigger more complex projects I should probably use `<br>` shouldn't I?
rufus2.innerHTML = ("You have just added to your collection:" + "<br/>");
varArray.forEach(e => rufus2.innerHTML += e);
//this cleans the input after executing the function
rufus1.value = '';
}
//this makes the function execute on an enter key press
document.addEventListener('keydown', function(enterKey) {
if (enterKey.keyCode === 13) {
arrayGiver();
}
})
。
<input id="rufus1">
<br>
<br>
<button onclick="arrayGiver();">Bleh</button>
<p id="rufus2"></p>
private void populateSpinner() {
lables = new ArrayList<String>();
quanty = new ArrayList<Integer>();
sl = new ArrayList<String>();
s_q = new ArrayList<String>();
s_pcode = new ArrayList<String>();
db=new DatabaseHandler(Offlineordupdate.this);
arrayList = db.getProductdetails();
arrayList2 = db.getsavedProductdetails();
int h=0;
if (arrayList.size() != 0) {
for (int i = 0; i < arrayList.size(); i++) {
sl.add(arrayList.get(i).getproductsl());
lables.add(arrayList.get(i).getproductname());
p_code.add(arrayList.get(i).getproductcode());
PROD_RATE.add(arrayList.get(i).getproductrate());
PROD_VAT.add(arrayList.get(i).getproductvat());
nameSerialPair.put(arrayList.get(i).getproductname(), Integer.parseInt(arrayList.get(i).getproductsl()));
if (arrayList2.size() != 0) {
for (int j = 0; j < arrayList2.size(); j++) {
s_pcode.add(arrayList.get(i).getproductcode());
s_q.add(arrayList2.get(j).getproductquant());
Log.d("Saved Product details",""+ s_pcode + s_q );
}
}
if( p_code.equals(s_pcode)) {
int p_serial = Integer.parseInt(arrayList.get(i).getproductsl());
mapQuantity.put(p_serial, String.valueOf(arrayList2.get(i).getproductquant()));
}
int p_serial = Integer.parseInt(arrayList.get(i).getproductsl());
mapQuantity.put(p_serial, String.valueOf(arrayList.get(i).getproductquant()));
}
}
if (arrayList2.size() != 0) {
for (int j = 0; j < arrayList2.size(); j++) {
mapQuantity.put(p_serial, String.valueOf(arrayList.get(j).getproductquant()));
}
}
adapter = new OfflineProductListupdateAdapter(Offlineordupdate.this, sl ,lables, mapQuantity);
productListView.setAdapter(adapter);
}
答案 1 :(得分:0)
因为数组在转换为字符串时自动将逗号添加为分隔符。您只需在join(' ')
上使用varArray
,就可以摆脱这种情况,如下所示
let varArray = [];
let rufus1 = document.getElementById('rufus1');
let rufus2 = document.getElementById('rufus2');
// variables for easing syntax
function arrayGiver() {
varArray.push('<div>' + document.getElementById('rufus1').value + '</div>') // I've used `<div>` instead of `<br>` for line breaking the elements, but when I'll be making bigger more complex projects I should probably use `<br>` shouldn't I?
rufus2.innerHTML = ("You have just added to your collection:" + "<br/>" +
varArray.join(' '));
//this cleans the input after executing the function
rufus1.value = '';
}
//this makes the function execute on an enter key press
document.addEventListener('keydown', function (enterKey) {
if (enterKey.keyCode === 13) {
arrayGiver();
}
})
<input id="rufus1" type="text">
<br>
<br>
<button onclick="arrayGiver();">Bleh</button>
<p id="rufus2"></p>
答案 2 :(得分:0)
要达到预期的结果,请使用字符串变量而不是数组,如下所示
let varArray = [];
var list ='';
let rufus1 = document.getElementById('rufus1');
let rufus2 = document.getElementById('rufus2');
// variables for easing syntax
function arrayGiver(){
list = list + '<div>' + document.getElementById('rufus1').value + '</div>'
rufus2.innerHTML = ("You have just added to your collection:" + "<br/>" + list);
list.value = '';
}
https://codepen.io/nagasai/pen/bvQKbX
let varArray = [];
var list ='';
let rufus1 = document.getElementById('rufus1');
let rufus2 = document.getElementById('rufus2');
// variables for easing syntax
function arrayGiver(){
list = list + '<div>' + document.getElementById('rufus1').value + '</div>'
rufus2.innerHTML = ("You have just added to your collection:" + "<br/>" + list);
list.value = '';
}
document.addEventListener('keydown', function(enterKey) {
if(enterKey.keyCode === 13){
arrayGiver();
}
})
&#13;
body {
font-family: Arial;
font-size: 24px;
}
button {
width: 100px;
height: 25px;
}
&#13;
<input id="rufus1">
<br>
<br>
<button onclick="arrayGiver();">Bleh</button>
<p id="rufus2"></p>
&#13;