从textarea清除硬编码文本

时间:2011-02-04 20:04:41

标签: php javascript html textarea

我正在尝试清空一个用php填充数据库的textarea。

以下是代码:

<select name="facility" id="facility">
    <option></option>
    <?php
    global $__CMS_CONN__;
    $sqlqry = "SELECT * FROM facility_db";
    $stmt = $__CMS_CONN__->prepare($sqlqry);
    $stmt->execute();
    while($row = $stmt->fetchObject())
    {
        echo "<option value=\"$row->id\">$row->facility</option>";
    }
    ?>
</select>
<button type="button" onclick="addFacility()">Add</button><button type="button" onclick="reset()">Reset</button>
<textarea readonly="readonly" id="facilities" name="facilities" rows="10" cols="50" value="<?php echo $facilitylist; ?>" ><?php echo $facilitylist; ?></textarea>
<input type="text" id="facilityids" name="facilityids" value="<?php echo $facilities; ?>"/>

从数据库填充选择框,Add按钮将选定的工具添加到textarea,将id添加到隐藏字段以进行数据库存储。

到目前为止,一切都很美妙。重置按钮将清除textarea和隐藏字段并重新开始。如果用户保存了他们的个人资料并选择了一些设施,就会出现问题。重置按钮仅适用于添加,但不会清除从数据库中提取的任何内容。

重置的javascript只是将那些字段id的值设置为空。

function reset()
{
    document.getElementById('facilities').value = null;
    document.getElementById('facilityids').value = null;
}

但它也不适用于隐藏领域。我将其设置为文本进行测试,即使我手动删除文本框中的内容,如果单击重置按钮,它也会回来。

发生了什么事?

2 个答案:

答案 0 :(得分:2)

给该函数另一个名字。表单中有一个预定义的方法reset(),该函数恢复表单字段的初始值。

按钮是一个表单字段,因此调用其表单的reset()方法而不是自定义方法。

答案 1 :(得分:1)

function reset(){
   document.getElementById('facilities').value= '';
    document.getElementById('facilityids').value = '';
}
textarea的

可能必须innerHTML = '';

正如dr molle所说,默认重置方法可能已用于重置()

reset method

相关问题