如何使用javascript隐藏表单的选项

时间:2016-04-22 21:21:23

标签: javascript php wordpress

我有一个特殊问题。我正在使用一个主题,该主题使用分类法将用户帖子分配给用户的板。因此,每个用户都可以创建自己的电路板并将其帖子分配给一个电路板。我正在使用wordpress frontent uploader并且可以在分类板上添加一个下拉表单。这很好。

我只有一个问题。用户可以在下拉列表中选择每个人的董事会。

分类法结构如下所示:

Parent: User ID 
Child: boardname

所以我的输出看起来像这样:

<div class="wpuf-fields wpuf_board_select_123_321">

<select data-required="yes" data-type="select" name='board[]' id='board[]' class='board wpuf_board_123' >
    <option value='-1'>&#8212; Select &#8212;</option>
    <option class="level-0" value="3">1</option>            // value is term ID // Content is User ID // parent
    <option class="level-1" value="17">User Board </option> // value is term ID // Content is Boardname
    <option class="level-1" value="26">User Board</option>  // value is term ID // Content is Boardname
    <option class="level-1" value="106">User Board</option> // value is term ID // Content is Boardname
    <option class="level-1" value="62">User Board</option>  // value is term ID // Content is Boardname
    <option class="level-1" value="148">User Board</option> // value is term ID // Content is Boardname
    <option class="level-0" value="191">10</option>         // value is term ID // Content is User ID // User has no boards
    <option class="level-0" value="193">11</option>         // value is term ID // Content is User ID // User has no boards
    <option class="level-0" value="10">2</option>           // value is term ID // Content is User ID // parent
    <option class="level-1" value="123">User Board</option> // value is term ID // Content is Boardname
    <option class="level-1" value="124">User Board</option> // value is term ID // Content is Boardname
    <option class="level-1" value="192">User Board</option> // value is term ID // Content is Boardname
    <option class="level-1" value="121">User Board</option> // value is term ID // Content is Boardname
    <option class="level-1" value="155">User Board</option> // value is term ID // Content is Boardname
    <option class="level-0" value="226">20</option>         // value is term ID // Content is User ID // User has no boards
</select>

</div>

所以这就是我到目前为止所做的:

 $board_parent_id = get_user_meta($user_id, "_Board Parent ID", true);
 $board_children_count = wp_count_terms("board", array( "parent" => $board_parent_id));

我的想法是使用javascript添加&#34;隐藏&#34;来隐藏不需要的选项。 所以我已经有父母身份证和孩子们数了,我现在正在努力,如何走得更远。

我希望有人能帮助我找到解决方案。

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果用户使用wp-admin,我不清楚。但是,我假设您的问题发生在wp-admin。

要使用javascript隐藏,您可以执行以下操作:

// Add to footer on admin a function to exec the scripts
add_action('admin_footer', 'my_custom_scripts');

// The function
function my_custom_scripts() {
    // An array of IDs that must be removed
    $itemsIdsToHide = array("121", "226", "123");

    // Create a string to convert the PHP array to JS array
    $strJavascriptArray = "Array('" . implode("','", $itemsIdsToHide) . "')";
    ?>
    <script>
        // Echoes the javascript array created in PHP
        var arr = <?php echo $strJavascriptArray; ?>;

        // Loop into each item of select.
        // BE SURE TO QUERY ONLY THE SELECT YOU NEED
        jQuery('select>option').each(function() {

            // IF the option ID is in the array to remove, remove the item
            if(arr.indexOf($(this).attr("value")) > -1) {
                $(this).remove();
            }
        });
    </script>
    <?
}

希望有所帮助:)