与将urls/文本按空格拆分为 2 个框 Javascript 相关,仅无库
我想要做的是拆分文本,它已经在文本框中。我希望将其放入变量中。然后,因为有 2 个盒子,我想要第一个盒子填充 1 2 3 4 5。第二个盒子填充 6 7 8 9 10。或者第一个盒子填充 1 3 5 7 9,第二个盒子填充 2 4 6 8 10. 无论哪种方式效果最好/最简单。
这是我到目前为止所尝试的。
代码:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
var split_text;
var urls_1;
var numbercount=0;
var totals;
var half_totals;
function addto_boxes(form) {
split_text = document.getElementById("text_to_split").value;
urls_1 = split_text.split(" ", 100000);
totals = urls_1.lenght - 1;
half_totals = (totals / 2 );
while (numbercount <= half_totals ){
if (numbercount >= half_totals ){
numbercount = 0;
break;
document.getElementById("input_box1").value = document.getElementById("input_box1").value + urls_1[numbercount] + " ";
document.getElementById("input_box2").value = document.getElementById("input_box2").value + urls_1[numbercount + half_totals] + " ";
numbercount++
}
}
</SCRIPT>
</HEAD>
<BODY>
<input id="Split" type="button" value="Add to boxes" onclick="addto_boxes(this.form);"/>
<BR>
<textarea NAME="texttosplit" id="text_to_split" VALUE="" rows="4" cols="75">1 2 3 4 5 6 7 8 9 10 </textarea>
<BR>
<FORM NAME="myform" ACTION="" METHOD="GET">Put 1st urls in this box (Separated by a space):
<textarea NAME="inputbox" id="input_box1" VALUE=""rows="4" cols="75"></textarea>
<BR>
Put 2nd urls in this box <BR>
<textarea NAME="inputbox2" id="input_box2" VALUE=""rows="4" cols="75"></textarea>
<BR>
<INPUT TYPE="reset">
</FORM>
</BODY>
</HTML>