简体   繁体   English

在HTML按钮上启用和禁用下拉菜单单击

[英]Enable and Disable Dropdown on HTML Button Click

I currently have a multiple row and column table with one of the columns containing dropdowns and one of the rows containing an Edit/Save button. 我目前有一个多行和多列表,其中一列包含下拉列表,其中一列包含“编辑/保存”按钮。 The dropdowns in each row of my table are disabled on load. 表格的每一行中的下拉菜单在加载时均被禁用。 However, on the button click, that dropdown in that row is then enabled. 但是,单击按钮后,将启用该行中的下拉菜单。 Once the button is clicked again (which is then a save button) they should then be disabled. 一旦再次单击该按钮(即保存按钮),则应将其禁用。

I have it working, but it is only working for the top row as I am using getElementById . 我有它的工作,但它只适用于第一行,因为我正在使用getElementById Everything I have tried for classes including getElementByClassName is not working for me. 我尝试过的所有类(包括getElementByClassName )对我来说都不起作用。

I have found multiple examples of what I am trying to do, but they all seem to be dealing with just one dropdown, whereas I have many that I am dealing with. 我已经找到了许多我想做的事的例子,但它们似乎都只涉及一个下拉菜单,而我有许多正在处理的问题。

So how can I get this to successfully work so that it will work for the corresponding row in which the button is pressed and not just the first row? 那么,如何才能使它成功工作,使其适用于按下按钮的相应行,而不仅仅是第一行?

HTML Table: HTML表格:

<table id="skuTable" cellspacing="5" class="ui-widget ui-widget-content">
    <thead>
        <tr class="ui-widget-header">
            <th style="display: none">Product ID</th>
            <th class="skuRow">Major Category</th>
            <th class="skuRow">Minor Category</th>
            <th class="skuRow">Report Code</th>
            <th class="skuRow">SKU</th>
            <th class="skuRow">SKU Description</th>
            <th class="skuRow">SKU Status</th>
            <th class="skuRow">Create Date</th>
            <th class="skuRow">SKU Group</th>
            <th class="skuRow">Edit</th>
        </tr>
    </thead>
    <tbody>

        <?php foreach ($dbh->query($query) as $row) {?>

        <tr>
            <td style="display: none" class="prod_id" id="product_id-<?php echo intval ($row['Product_ID'])?>"><?php echo $row['Product_ID']?></td>
            <td class="major_cat" id="major_cat-<?php echo intval ($row['Major Category'])?>"><?php echo $row['Major Category']?></td>
            <td class="minor_cat" id="minor_cat-<?php echo intval ($row['Minor Category'])?>"><?php echo $row['Minor Category']?></td>
            <td class="rep_code" id="rep_code-<?php echo intval ($row['Product Report Code'])?>" align="center"><?php echo $row['Product Report Code']?></td>
            <td class="sku" id="sku-<?php echo intval ($row['SKU'])?>" align="center"><?php echo $row['SKU']?></td>
            <td class="sku_desc" id="sku_desc-<?php echo intval ($row['SKU Description'])?>"><?php echo $row['SKU Description']?></td>
            <td class="sku_status" id="sku_status-<?php echo intval ($row['SKU Status'])?>" align="center"><?php echo $row['SKU Status']?></td>
            <td class="create_date" id="create_date-<?php echo intval ($row['Date'])?>" align="center"><?php echo $row['Date']?></td>
            <td class="sku_group" id="sku_group-<?php echo intval ($row['SKU Group'])?>" align="center">

                <div>
                <select id="sku_group_dropdown" disabled>
                    <option
                        value=""
                        data-name="<?php echo $row ['SKU Group'];?>"
                    >
                        <?php echo $row ['SKU Group'];?>
                    </option>
                </select>
            </div>

                <!--<div><?php echo $row ['SKU Group'];?></div>-->
            </td>
            <td><input type="button" class="edit" name="edit" value="Edit"></td>
        </tr>

    <?php } ?>

    </tbody>
</table>

Lines of JavaScript I use to switch from disabled to enabled and vice versa. 我使用的JavaScript行从禁用状态切换为启用状态,反之亦然。 These are posted in different areas of my JS, but am only posting these 2 lines as they are the only lines of code that apply to my question: 这些发布在我的JS的不同区域中,但是只发布了这两行,因为它们是适用于我的问题的唯一代码行:

document.getElementById("sku_group_dropdown").disabled=false;
document.getElementById("sku_group_dropdown").disabled=true;

please check this code 请检查此代码

    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
 <table id="skuTable" cellspacing="5" class="ui-widget ui-widget-content">
<thead>
    <tr class="ui-widget-header">
        <th style="display: none">Product ID</th>
        <th class="skuRow">Major Category</th>
        <th class="skuRow">Minor Category</th>
        <th class="skuRow">Report Code</th>
        <th class="skuRow">SKU</th>
        <th class="skuRow">SKU Description</th>
        <th class="skuRow">SKU Status</th>
        <th class="skuRow">Create Date</th>
        <th class="skuRow">SKU Group</th>
        <th class="skuRow">Edit</th>
    </tr>
</thead>
<tbody>



   <tr>
     <td>Test1</td>
     <td>
        <div>
            <select  disabled class="drop-down-list">
                <option
                    value=""
                    data-name="asdas">
                    Tesy1
                </option>
            </select>
        </div>


        </td>
        <td><input type="button" class="edit" onclick="enable_value(this)" name="edit" value="Edit"></td>
    </tr>

 <tr>
     <td>Test2</td>
     <td>
        <div>
            <select  disabled class="drop-down-list">
                <option
                    value=""
                    data-name="asdas">
                    Tesy2
                </option>
            </select>
        </div>


        </td>
        <td><input type="button" class="edit" onclick="enable_value(this)" name="edit" value="Edit"></td>
    </tr>

</tbody>
</table>

and this your function 这是你的功能

 <script>
    function enable_value(event){
        if($(event).parent().parent().find('.drop-down-list').prop("disabled")){
            $(event).parent().parent().find('.drop-down-list').attr('disabled', false);
        } else {
             $(event).parent().parent().find('.drop-down-list').attr('disabled', true);
        }
    }
</script>

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

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