0

我正在尝试实现这样的功能

  1. 如果我按,non2然后打印text2textarea2
  2. 如果我然后按,pos2然后打印text2textarea1

这里我想要的是当我按下pos2then 时将被打印text2textarea1并且text2intextarea2将被删除。相反,如果我pos2先击中non2然后再击中,则仅text2textarea2.

我正在考虑使用 if-else 语句来查看是否单击了 POS 或 NON 之一。这就是我最终的结果:

<?php
    for ($j = 1; $j <= 3; $j++) {  
        echo '
            <div id="part'. $j .'" >
                <div id="teks'.$j.'">TEXT 2 </div>
                <input type="radio" id="pos'. $j .'" name="teks'.$j.'" value="POS" onclick="addPOS(this.previousElementSibling.innerHTML);">POS2</input>
                <input type="radio" id="non'. $j .'" name="teks'.$j.'" value="NON" onclick="addNON(this.previousElementSibling.previousElementSibling.innerHTML);">NON2</input>
                <input type="radio" id="neg'. $j .'" name="teks'.$j.'" value="NEG" checked="checked" onclick="delPOS(document.getElementById(\'teks'.$j.'\').innerHTML); delNON(document.getElementById(\'teks'.$j.'\').innerHTML);">NEG2</input>
            </div>
        ';
    }
?>   
<script type="text/javascript">
    function delPOS(elem)
    {
        var txt = document.getElementById("posbox").value;
        if (txt.length>elem.length){
            document.getElementById("posbox").value = txt.substr(0,txt.length-elem.length-1);
        }else{
            document.getElementById("posbox").value="";
        }
    }
    function delNON(elem)
    {
        var txt = document.getElementById("nonbox").value;
        if (txt.length>elem.length){
            document.getElementById("nonbox").value = txt.substr(0,txt.length-elem.length-1);
        }else{
            document.getElementById("nonbox").value="";
        }
    }
    function delNEG(elem)
    {
        var txt = document.getElementById("negbox").value;
        if (txt.length>elem.length){
            document.getElementById("negbox").value = txt.substr(0,txt.length-elem.length-1);
        }else{
            document.getElementById("negbox").value="";
        }
    }

    function addPOS(elem)
    {
        document.getElementById("posbox").value+=elem;
    }
    function addNON(elem)
    {
        document.getElementById("nonbox").value+=elem;
    }
    function addNEG(elem)
    {
        document.getElementById("negbox").value+=elem;
    }
</script>
<div id="box" >
    <textarea style="width:420px" name="posbox" id="posbox" rows="4" cols="70"></textarea>
    <textarea style="width:420px" name="nonbox" id="nonbox" rows="4" cols="70"></textarea>
    <textarea style="width:420px" name="negbox" id="negbox" rows="4" cols="70"></textarea>
</div>

但是,这不起作用。我做错了什么,我该如何解决?

4

2 回答 2

0

make it simpler. Assumed that radios "name" is the same as the divs id where you take the content from:

...
<div id="teks'.$j.'">TEXT 2 </div>
<input type="radio" id="pos'. $j .'" name="teks'.$j.'" value="POS" 
   onclick="addPOS(this);">POS2</input>
<input type="radio" id="non'. $j .'" name="teks'.$j.'" value="NON" 
   onclick="addNON(this);">NON2</input>
...

you could just do:

function addPOS(elem)
{
    document.getElementById("posbox").value+=document.getElementById(elem.name).innerHTML;
}
于 2013-07-13T03:04:09.300 回答
0

你把这弄得太复杂了,你所要做的就是创建一个函数,例如Print() 你定义它的地方看起来像这样:

function Print(what,textareaobj)
{ 
  textareaobj.value=what
}

然后在您正在调用的文件中设置例如什么变量

var what = "The text";

并为按钮添加属性 onclick="Print(what,getElementById('idoftextarea')" 例如:

<button onclick="Print(what,getElementById('idoftextarea')">Click</button>
于 2013-07-13T03:35:26.510 回答