【问题标题】:enable/ disable select field based on label (radio) selection根据标签(单选)选择启用/禁用选择字段
【发布时间】:2019-04-16 20:41:27
【问题描述】:

我正在尝试禁用和启用基于 pdo-query 生成的单选输入标签的选择字段,但无法正确执行。

选择字段确实被 prop('disabled', true) 禁用,但返回启用状态不起作用。 (代码注释块中的示例标签列表)

我尝试了各种方法,onClick、onChange 等,但似乎都没有按预期工作。我还在这里搜索了有关此问题的各种主题,但所有这些显然都在使用无线电输入本身并带有具体选项。例如,我之前咨询过的这个线程

Enable/Disable a button on select of radio button in jquery

我的情况是我有一个带有动态标签的不确定无线电输入字段,所以我不能使用像if (val = this || val = that)这样的具体比较

请参考下面的代码和我预期的解决方案

<form name='enter_slip_data' action='submitPatData.php' method='post' class='needs-validation' novalidate>
    <div class='form-group'>
        <select name='doctor_name' id='doctor_name_id' class='custom-select' required>
            <option selected disabled value=''>Select Doctor</option>

            <!-- function below will print options of doctor names fetched from database -->
            <?php echo getDocNameList(); ?>
        </select>

    </div>
    <div class='form-group'>
        <input type='text' name='patient_name' class='form-control' placeholder='Enter Patient Name' required/>
    </div>
    <div class='form-group'>
        <input type='text' name='patient_telephone' class='form-control' placeholder='Enter Patient Telephone' required/>
    </div>
    <div class='form-group'>
        <input type='text' name='patient_address' class='form-control' placeholder='Enter Patient Address' required/>
    </div>
    <div class='form-group'>
        <input type='text' name='patient_age' class='form-control' placeholder='Enter Patient Age' required/>
    </div>
    <div class='form-group'>
        <select name='patient_gender' class='custom-select' required>
            <option selected disabled>Select Gender</option>
            <option  value='male'>Male</option>
            <option  value='female'>FeMale</option>
        </select>
    </div>
    <div class='form-group'>
        <div class='btn-group btn-group-toggle' style='flex-flow: row wrap;border-radius:5px;overflow:hidden;' data-toggle='buttons'>
            <?php
                /* function below will fetch records from database and print labels with radio inputs
                    These inputs could be of any number with at least one of them named 'Drip Case'

                    i-e-
                    ------------------------------------------------------
                    | Drip Case | Histatmy | Sk. Stitch | LSCS | ND & DNC |
                    ------------------------------------------------------

                    Clicking on drip case disables the above select box
                    Clicking on any other label should enable it back
                */
                function getOperationList(){
                    global $db;
                    $getOPType = $db->prepare("SELECT * from operation_types");
                    $getOPType->execute();
                    $opType = '';
                    if($getOPType->rowCount() > 0){
                        while($showOpTypes = $getOPType->fetch(PDO::FETCH_ASSOC)){
                            $opType .=  "<label id='opType_label'class='btn btn-secondary success' style='border-radius:0;'>";
                            $opType .= "<input type='radio' id='op_type' name='op_type' value='".$showOpTypes['id']."' autocomplete='off'>" . $showOpTypes['name'];
                            $opType .= "</label>";
                        }
                    return $opType;
                    }
                }
            ?>
        </div>
    </div>
    <div class='form-group'>
        <input type='text' name='patient_fee' class='form-control' placeholder='Enter Fees' required/>
    </div>

    <div class='form-group'>
    <input type='hidden' name='dept' id='dept' value='Operation' />
    <input type='hidden' name='test_type' value='2' />

    <button type='button' class='btn btn-secondary' disabled><?php echo getReciptNo(); ?></button>
    <button type='submit' name='submitPatData' class='btn btn-secondary' value='Print Recipt'>Print Recipt</button>
    <button type='button' class='btn btn-danger' data-dismiss='modal'>Cancel</button>
</div>
</form>

我现在尝试使用的 jQuery 代码是

    $('#opType_label').click(function(){
        if($(this).text() == 'Drip Case'){
            $('#doctor_name_id').prop("selectedIndex", 0);
            $('#doctor_name_id').prop("disabled", true);
        } else {
            $('#doctor_name_id').prop("disabled",false);
        }


    });

使用上面的代码,我可以在单击 Drip-Case 标签时禁用选择框,但当我单击任何其他标签时它不会重新启用,这是我的预期结果

【问题讨论】:

  • 我认为你想使用更改,而不是点击。
  • 哦,您还需要做其他事情才能获得选择的值...stackoverflow.com/questions/10659097/…
  • 也有可能因为 html 是动态生成的,所以 jquery 无法在 dom 中找到它。试试$(document).on('click', '#opType_label' function() {//your code here});
  • 我看到的最大问题是您对每个单选标签都使用了#opType_label ID。您需要为每个 ID 设置一个唯一 ID,或者改为使用一个类。
  • @DanielG 哦,我的天哪,我怎么会如此盲目地没有注意到这个愚蠢的错误。将其更改为使用类而不是 ID,突然间一切都与上面提到的 jQuery 一样完美

标签: jquery onclick


【解决方案1】:

我看到的最大问题是您对每个单选标签都使用了#opType_label ID。你需要一个唯一的 ID 或使用一个类来代替。- DanielG

正如丹尼尔所说,我使用类而不是 od ID,它现在可以工作了。

while($showOpTypes = $getOPType->fetch(PDO::FETCH_ASSOC)){
     $opType .=  "<label id='opType_label'class='btn btn-secondary success opType' style='border-radius:0;'>";
     $opType .= "<input type='radio' id='op_type' name='op_type' value='".$showOpTypes['id']."' autocomplete='off'>" . $showOpTypes['name'];
     $opType .= "</label>";
}
  $('.opType').click(function(){
        if($(this).text() == 'Drip Case'){
            $('#doctor_name_id').prop("selectedIndex", 0);
            $('#doctor_name_id').prop("disabled", true);
        } else {
            $('#doctor_name_id').prop("disabled",false);
        }


    });

【讨论】:

    猜你喜欢
    • 2013-12-14
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 2012-02-23
    • 2016-07-12
    • 1970-01-01
    • 2018-09-19
    相关资源
    最近更新 更多