javascript - PrototypeJS 不删除选择元素

标签 javascript prototypejs

鉴于以下 HTML,我尝试删除所有表单元素。我遇到的问题是 select 元素没有被删除,而是每次 remove 时都会删除其中的第一个 option > 代码被调用。 请参阅http://jsfiddle.net/b8FfT/

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');

最佳答案

因此 HTMLSelectElement 原型(prototype)(不是框架)有自己的 remove()方法以及当您调用 remove() 时上<select>它不会沿着原型(prototype)链向上遍历到 remove() PrototypeJS 添加的 HTMLElement 方法。

给你2个选择

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

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

我也为此提交了错误报告

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

编辑

使用 CSS 选择器和 select()像这样的方法

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

关于javascript - PrototypeJS 不删除选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19684961/

相关文章:

javascript - 使用 Prototype 将窗口滚动到沿 y 轴居中?

javascript - 包含小写、大写、字母数字、特殊字符的正则表达式,且一行中相同字符不超过 2 个,最小长度为 8 个字符

javascript - Img 在点击时扩展到 100%(并且仍然坚持网格)

javascript - Prototypejs 与 Underscore 冲突

javascript - 有没有办法取回在 Prototype 中使用 Event.observe 注册的匿名事件处理程序?

javascript - HTML5 Canvas : Create a Image. onload() 函数通过 Image.prototype

javascript - PrototypeJS 循环不会运行

javascript - 415 在 React js 中使用 axios post 请求时不支持的媒体类型

Javascript 函数与使用前预定义的类(特别是 angularjs)

javascript - 我在这里把问题括起来了吗?或者真的有一个意想不到的标识符? Javascript/jQuery