根据标签(无线电)选择启用/禁用选择字段

时间:2019-04-16 20:41:28

标签: jquery onclick

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

select字段确实被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标签时禁用选择框,但是当我单击任何其他预期效果的标签时,都不会启用选择框

1 个答案:

答案 0 :(得分:0)

  

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

正如丹尼尔(Daniel)所述,我使用了类而不是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);
        }


    });