简体   繁体   English

从 Select 盒子中取出物品

[英]Remove Item from Select Box

I'm trying to remove an item (value=155 / 'Fruits') from a select box drop down.我正在尝试从 select 下拉框中删除一个项目(值 = 155 / 'Fruits')。

    <div class="control select">
      <select id="product_cat"
        name="product_cat[]"
        class="category-select2 select2-hidden-accessible"
        style=""
        data-parsley-maxcheck="3"
        multiple=""
        data-parsley-multiple="product_cat[]"
        tabindex="-1"
        aria-hidden="true"
      >
        <option class="level-0" value="115">Misc</option>
        <option class="level-0" value="161">Vegetables</option>
        <option class="level-0" value="160">Grains</option>
        <option class="level-0" value="155">Fruits</option>
        <option class="level-1" value="156">&nbsp;&nbsp;&nbsp;Apples</option>
        <option class="level-1" value="157">&nbsp;&nbsp;&nbspBananas</option>

I'm using Wordpress and after many attempts I haven't been able to remove it from the dropdown.我正在使用 Wordpress,经过多次尝试,我无法将其从下拉列表中删除。 For example, I've tried the following JS code in my function.php file:例如,我在function.php文件中尝试了以下 JS 代码:

    function customcats_hook_javascript() {
        ?>
            <script>
    var select = document.getElementById('product_cat')
    select.removeChild(select.querySelector('option[value="155"]'))
            </script>
        <?php
    }
    add_action('wp_head', 'customcats_hook_javascript');

There is possibly a problem with your script executed in before all body elements are available.在所有正文元素可用之前执行的脚本可能存在问题。 Normally you need to wait until HTML is processed and DOM is complete.通常你需要等到 HTML 被处理并且 DOM 完成。 One possibility is moving script to the end of the page, somewhere in the footer.一种可能性是将脚本移动到页面的末尾,在页脚的某处。 Ideally need to wait until page loads, with jQuery loaded you can use理想情况下需要等到页面加载,加载 jQuery 后,您可以使用

jQuery('document').ready(function(){
// your code here
});

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

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