选中/未选中动态填充/取消填充Textarea

时间:2013-01-25 00:58:47

标签: checkbox textarea

我正在尝试使用复选框用特定文本填充textarea;但是,当取消选中该复选框时,我希望删除填充的文本。我试过这个:

$('input:checkbox').click(function(){
var tb = "#"+$(this).attr('rel');
if($(this).is(":checked"))
$(tb).append(this.name + "\n"); 
else($(this).not(":checked"))
$(tb).remove(this.name + "\n");
});

无济于事。 HALP?

EDIT
主要向前迈进......我已经想出如何重新设计剧本,以便几乎完全按照我的需要行事。我把上面改为:

$('input:checkbox').click(function(){
var tb = "#"+$(this).attr('rel');
if($(this).is(":checked"))
$(tb).append(this.name + "\n")
else($(tb).empty(this.name + "\n"))
});

我现在唯一的问题是,当我取消选中一个复选框时,它会清除文本框中的所有文字,当我只想要清除特定文本时未选中的复选框。

1 个答案:

答案 0 :(得分:0)

尝试下面的代码,它会帮助你......

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>                     

<script type="text/javascript">
    $(document).ready(function() {
$("input[type=checkbox]").click(function() {
    if ($(this).is(':checked'))
    $('#purchased').html($('#purchased').val() + $(this).attr('name') + "\n");
else
{
    var text = $('#purchased').val();
    text = text.replace($(this).attr('name') + "\n" ,"");
    $('#purchased').html(text);
}
  });
});
</script>
</head>
<body>
<textarea id="purchased" cols="25" rows="10"></textarea>
        <p>Pennsylvania State Standards, Subject 1: Reading, Writing, Speaking, and Listening</p>
        <ul>
            <li><input type="checkbox" rel="textbox" name="1.1. Reading Independently">1.1. Reading Independently</input></li>
            <li><input type="checkbox" rel="textbox" name="1.2. Reading, Analyzing, and Interpreting Text">1.2. Reading, Analyzing, and Interpreting Text</input></li>
            <li><input type="checkbox" rel="textbox" name="1.3. Reading, Analyzing, and Interpreting Literature: Fiction and Non-Fiction">1.3. Reading, Analyzing, and Interpreting Literature: Fiction and Non-Fiction</input></li>
            <li><input type="checkbox" rel="textbox" name="1.4. Types of Writing">1.4. Types of Writing</input></li>
            <li><input type="checkbox" rel="textbox" name="1.5. Quality of Writing">1.5. Quality of Writing</input></li>
            <li><input type="checkbox" rel="textbox" name="1.6. Speaking and Listening">1.6. Speaking and Listening</input></li>
            <li><input type="checkbox" rel="textbox" name="1.7. Characteristics and Functions of the English Language">1.7. Characteristics and Functions of the English Language</input></li>
            <li><input type="checkbox" rel="textbox" name="1.8. Research">1.8. Research</input></li>
            <li><input type="checkbox" rel="textbox" name="1.9. Information, Communication, and Technology Literacy">1.9. Information, Communication, and Technology Literacy</input></li>
        </ul>
</body>
</html>