jQuery - 清除textarea文本内容

时间:2018-01-24 11:22:17

标签: javascript php jquery html

如何使用“onclick”清除textarea内容?

<div class="form-group col-sm-12">
    <label class="control-label col-sm-3">System Main Message</label>
    <div class="col-sm-4">
        <textarea id="editor-area" name="sysMsg" class="form-control"><?PHP echo $mainIndex['sysMsg'] ?></textarea>
        <a href="#" onclick="document.getElementById['editor-area'].value = ''">Clear message</a>
    </div>
</div>

3 个答案:

答案 0 :(得分:1)

使用它。它将删除文本区域的所有内容

&#13;
&#13;
$( "#target" ).click(function() {
  $('#editor-area').val('');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="editor-area">
  test test test
</textarea>
<button id="target">Clear</button>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您使用的括号不正确。将document.getElementById['editor-area']更改为document.getElementById('editor-area'),然后就可以了:

<div class="form-group col-sm-12">
    <label class="control-label col-sm-3">System Main Message</label>
    <div class="col-sm-4">
        <textarea id="editor-area" name="sysMsg" class="form-control">Textarea value</textarea>
        <a href="#" onclick="document.getElementById('editor-area').value =''">Clear message</a>
    </div>
</div>

答案 2 :(得分:0)

你可以在Jquery中使用empty()个方法。

 $( "#clear" ).click(function() {
     $('#editor-area').empty();
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group col-sm-12">
    <label class="control-label col-sm-3">System Main Message</label>
    <div class="col-sm-4">
        <textarea id="editor-area" name="sysMsg" class="form-control">testing text</textarea>
        <a id="clear" href="#">Clear message</a>
    </div>
</div>