1

我正在尝试创建一个网页

  1. 如果用户点击一个单词,它会显示在一个文本框中
  2. 如果一个单词被点击两次,它不会再次出现在框中
  3. 用户可以通过一个按钮清除该框
  4. 用户可以将内容发送到另一个页面

我的代码如下:

<div id="parent">
    <!-- get the clicked word and add it to textarea-->
    <script type="text/javascript">
        $("#parent").delegate("span", "mousedown", function() {
            if ( $("#thediv").text().indexOf($(this).text()) <0 )
            {
                $("#thediv").append($(this).text());// get old div contents and add clicked word
                $("#thediv").append(' ');// also add a space
                $("#thediv").html(); //print to div
            };   
       })
    </script>

    <!-- clickable words  -->    
    <span>One </span><span>Two</span><span>Three</span>

    <!-- the form --> 
    <form action="/test/index6.html" method="get">
        <textarea id="thediv" name="thediv"></textarea><br>
        <input type="submit" >
    </form>

    <!-- Clear button  -->     
    <input type="button" id="clear" value="Clear" />
    <script type="text/javascript">
        $('#clear').click(function (){
            $("#thediv").val('');
        });
    </script>
</div>

它工作正常,在鼠标单击时,单击的单词被存储(如果它位于标签之间。对于单击的每个单词,我检查文本区域是否已经包含它,如果没有,我添加它。

我的问题是清除按钮。它清除了文本区域,但是我不能再添加任何单词,就好像这部分代码停止工作一样。

有什么建议吗?

4

1 回答 1

1

尝试更改$("#thediv").val('');$("#thediv").html('');,就像在这个 jsfiddle中一样。

于 2013-10-24T22:11:09.817 回答