2013-10-30 63 views
1

鑑於以下HTML,我試圖刪除所有表單元素。我遇到的問題是select元素沒有被刪除,而是其中的第一個option正在被刪除,每次調用刪除代碼。 見http://jsfiddle.net/b8FfT/PrototypeJS不刪除選擇元素

HTML

<fieldset> 
    <div id="order_history_block"> 
    <div id="history_form" class="order-history-form"> 
     <div>Add Order Comments</div> 
     <span class="field-row"> 
     <label class="normal" for="history_status">Status</label><br> 
     <select name="history[status]" class="select" id="history_status"> 
      <option value="processing">Ok to Ship</option> 
      <option value="pending" selected="selected">Pending</option> 
     </select> 
     </span> 
     <span class="field-row"> 
      <label class="normal" for="history_comment">Comment</label> 
      <textarea name="history[comment]" rows="3" cols="5" style="height:6em; width:99%;" id="history_comment"></textarea> 
     </span> 
     <div class="f-left"> 
     <input name="history[is_visible_on_front]" type="checkbox" id="history_visible" value="1"><label class="normal" for="history_visible"> Visible on Frontend</label> 
     </div> 
     <div class="f-right"> 
     <button id="id_79ae3bd75916862b0245fbcb3343d24e" title="Submit Comment" type="button" class="scalable save" onclick="doStuff()" style=""><span><span><span>Submit Comment</span></span></span></button> 
     </div> 
     <div class="clear"></div> 
    </div> 
    <div class="divider"></div> 
    <!-- ... --> 
    </div> 
</fieldset> 

JS

var a = $('order_history_block').parentNode; 
$(a).select('input', 'select', 'textarea').invoke('remove'); 

回答

1

所以HTMLSelectElement原型(不是框架)都有自己的remove()方法,當你調用remove()<select>的IT不會遍歷原型鏈到remove()由PrototypeJS添加的HTMLElement方法。

2選項供您

$('history_status').parentNode.removeChild($('history_status')); 

Element.remove($('history_status')); 

我已經提交了一份bug報告本以及

https://github.com/sstephenson/prototype/issues/122

編輯

使用CSS選擇器和select()方法這樣

$('order_history_block').up().select('select').each(function(item){ 
    Element.remove(item); 
}); 
+0

看起來那會工作。我將如何使用'select()'的輸出來做到這一點?我想避免在那裏硬編碼'select'的ID。 – nachito

+0

我在上面添加了答案 –