2011-02-04 79 views
1

我試圖清空一個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; ?>"/> 

的選擇框從數據庫填充,並添加按鈕添加選擇的設施textarea的,而id的隱藏字段用於數據庫存儲。

到目前爲止,一切都很好。重置按鈕將清除textarea和隱藏字段並重新開始。問題出現在用戶保存了他們的個人資料並選擇了一些設施。重置按鈕僅適用於添加,但不會清除從數據庫中提取的任何內容。

重置的JavaScript只是將這些字段id的值設置爲空。

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

但它不適用於隱藏的領域。我將它設置爲文本進行測試,甚至當我手動刪除文本框中的內容時,如果單擊重置按鈕,它會返回。

這是怎麼回事?

+2

您是否嘗試過設置`value =''`? – 2011-02-04 20:12:45

回答

2

提供的功能的另一名默認的復位方法。在窗體內部有一個預定義的方法reset(),該函數恢復窗體字段的初始值。

一個按鈕是一個表單字段,通過此調用他的表單的reset() - 方法而不是您的自定義方法。

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

爲textarea的可能要做innerHTML = '';

爲DR躑躅說,可能已經使用了你的復位()

reset method

+0

試過了,它被設置爲在我嘗試null之前,並且innerHTML也沒有工作。 – Ryan 2011-02-04 20:34:31

+0

textarea的`.value`爲我工作。 – icedwater 2014-08-11 04:48:38