0

到目前为止,我还不能让它改变输入框的值。我想让它做什么?我希望它从第一个框中获取文本。当您单击按钮时,会将其拆分并将它们附加到第二个和第三个框。

我不想使用 jquery 或任何纯 javascript 库。

有什么问题可以问。

不知道我在这里做错了什么。似乎它应该工作。有什么帮助吗?谢谢 已编辑,这可以满足我的需要....不确定它是否是最好的方法,但确实有效 代码

    <HTML>
    <HEAD> 
    <SCRIPT LANGUAGE="JavaScript">
        var urls_1;
        var split_text;
    function addto_boxes(form) {
            var split_text = document.getElementById("text_to_split").value;
            var urls_1 = split_text.split(" ", 100000);
            document.getElementById("input_box1").value = document.getElementById("input_box1").value + urls_1[0] + " ";
            document.getElementById("input_box2").value = document.getElementById("input_box2").value + urls_1[1] + " ";
    }


    </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"></textarea><Br>


    <FORM NAME="myform" ACTION="" METHOD="GET">Put 1st urls in this box:
    <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>
4

1 回答 1

0

这应该适合你

HTML

<input id="Split" type="button" value="Add to boxes" />
<Br>
<textarea NAME="texttosplit" id="text_to_split" VALUE="" rows="4" cols="75"></textarea>
<Br>
<FORM NAME="myform" ACTION="" METHOD="GET">Put 1st urls in this box:
    <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 id="reset" TYPE="reset">
</FORM>

Javascript

var aBox1 = [];
var aBox2 = [];

document.getElementById("Split").addEventListener("click", function () {
    var urls_1 = document.getElementById("text_to_split").value.trim().split(" "),
        url1 = urls_1[0] || "",
        url2 = urls_1[1] || "";

    if (url1.length) {
        aBox1.push(url1);
    }

    if (url2.length) {
        aBox2.push(url2);
    }

    document.getElementById("input_box1").value = aBox1.join(" ");
    document.getElementById("input_box2").value = aBox2.join(" ");
}, false);

document.getElementById("reset").addEventListener("click", function () {
    aBox1.length = 0;
    aBox2.length = 0;
}, false);

jsfiddle 上

于 2013-04-29T22:23:54.123 回答