两个问题,奇怪的'undefined'和删除字符串,如果没有其他字符串

时间:2017-11-01 10:44:17

标签: javascript string input

if(foo.Any(item => bar.Contains(item)))
function button2() {
a1 = document.getElementById('line1').value;
a2 = document.getElementById('line2').value;
a3 = document.getElementById('line3').value;
a4 = document.getElementById('line4').value;
a5 = document.getElementById('line5').value;
a6 = document.getElementById('line6').value;
a7 = document.getElementById('line7').value;
a8 = document.getElementById('line8').value;
v = document.getElementById('linksC').innerHTML;
this.code1 = document.getElementById('affiliate').value;
this.cc;

if (a1 !== ' ' && this.code1 + a1) {this.cc += a1+this.code1 + '<br>'} else {this.code1 = ' ';};
if (a2 !== ' ') {this.cc += a2+this.code1 + '<br>'};
if (a3 !== ' ') {this.cc += a3+this.code1 + '<br>'};
if (a4 !== ' ') {this.cc += a4+this.code1 + '<br>'};
if (a5 !== ' ') {this.cc += a5+this.code1 + '<br>'};
if (a6 !== ' ') {this.cc += a6+this.code1 + '<br>'};
if (a7 !== ' ') {this.cc += a7+this.code1 + '<br>'};
if (a8 !== ' ') {this.cc += a8+this.code1 + '<br>'};

document.getElementById('linksC').innerHTML = this.cc;



}

第一个问题: 出于某种原因,我在输入文本之前得到了一个奇怪的未定义。如果我点击按钮来组合输入,我将得到这个打印未定义。

问题二: 如果我正在添加联盟代码,如果链接为空,我不希望它打印。

提前感谢您阅读本文。

1 个答案:

答案 0 :(得分:0)

cc;未被删除,您应该在将其他值连接到它之前为其分配值,例如cc = '';

同时删除if语句中单引号之间的空格,以检查它们是否为空字符串。

编辑:从@charlietfl

指出的变量中删除它

&#13;
&#13;
function button2() {
a1 = document.getElementById('line1').value;
a2 = document.getElementById('line2').value;
a3 = document.getElementById('line3').value;
a4 = document.getElementById('line4').value;
a5 = document.getElementById('line5').value;
a6 = document.getElementById('line6').value;
a7 = document.getElementById('line7').value;
a8 = document.getElementById('line8').value;
v = document.getElementById('linksC').innerHTML;
code1 = document.getElementById('affiliate').value;
cc = '';

if (a1 !== '' && code1 + a1) {cc += a1+code1 + '<br>'} else {code1 = '';};
if (a2 !== '') {cc += a2+code1 + '<br>'};
if (a3 !== '') {cc += a3+code1 + '<br>'};
if (a4 !== '') {cc += a4+code1 + '<br>'};
if (a5 !== '') {cc += a5+code1 + '<br>'};
if (a6 !== '') {cc += a6+code1 + '<br>'};
if (a7 !== '') {cc += a7+code1 + '<br>'};
if (a8 !== '') {cc += a8+code1 + '<br>'};

document.getElementById('linksC').innerHTML = cc;

}
&#13;
<p>
multiple links.</p>
<p>link 1</p>
<input type="text" id="line1">
<p>link 2</p>
<input type="text" id="line2">
<p>link 3</p>
<input type="text" id="line3">
<p>link 4</p>
<input type="text" id="line4">
<p>link 5</p>
<input type="text" id="line5">
<p>link 6</p>
<input type="text" id="line6">
<p>link 7</p>
<input type="text" id="line7">
<p>link 8</p>
<input type="text" id="line8">

<p>affiliate code insert</p>
<input type="text" id='affiliate'>
<br><br>
<button onclick="button2()">combine link with code</button> <br><br>
<div id="linksC"></div>
&#13;
&#13;
&#13;