简体   繁体   English

PrototypeJS不删除选择元素

[英]PrototypeJS not removing select element

Given the following HTML, I'm trying to remove all form elements. 给定以下HTML,我正在尝试删除所有表单元素。 The problem I'm encountering is that the select element is not being removed, but rather the first option in it is being removed each time the remove code is called. 我遇到的问题是select元素没有被删除,而是每次调用remove代码时都删除了它的第一个option See http://jsfiddle.net/b8FfT/ 参见http://jsfiddle.net/b8FfT/

HTML 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 JS

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

So the HTMLSelectElement prototype (not the framework) has its own remove() method and when you call remove() on <select> s it does not traverse up the prototype chain to the remove() method of HTMLElement added by PrototypeJS. 因此,HTMLSelectElement原型(不是框架)具有其自己的remove()方法,并且在<select>上调用remove() ,它不会遍历原型链到PrototypeJS添加的HTMLElement的remove()方法。

2 options for you 为您提供2种选择

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

or 要么

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

I've filed a bug report for this as well 我也为此提交了错误报告

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

EDIT 编辑

Use a CSS selector and the select() method like this 使用CSS选择器和select()方法,如下所示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM