简体   繁体   English

如何在下拉列表更改时动态隐藏表格行?

[英]How to hide table row dynamically on dropdown change?

Here I've a drop-down and when user changes this drop-down, I want to show only specific table rows eg if Patient Type=Inpatient then JQuery will show only those records .The table rows are dynamically generated by php code. 在这里我有一个下拉列表,当用户更改此下拉列表时,我只想显示特定的表行,例如,如果Patient Type = Inpatient,则JQuery将仅显示那些记录。表行由php代码动态生成。 Please suggest me the way i'm new in JQuery. 请建议我在JQuery中的新方式。 My drop-down is: 我的下拉菜单是:

<tr>
    <td style="text-align:left"><label>Patient Type:</label></td>
    <td>
    <select id="patient_type">
        <option>Select patient type</option>
        <option>InPatient</option>
        <option>OutPatient</option>
    </select>
    </td>
</tr>

The PHP code: PHP代码:

 <table class="draw_table" id="datawtable" border="1" width="100%" cellspacing="0" cellpadding="0"> <tr> <th> Patient_name </th> <th> Patient Type </th> <th> Payment Type </th> <th> Date </th> <th>Amount</th> </tr> <?php $query="SELECT doctor.doctor_name,inovoice.patient_name,inovoice.patient_type,inovoice.payment_type,inovoice.inovoice_date,inovoice.inovoice_amount from doctor,inovoice,prescription where doctor.doctor_name=prescription.doctor_name and inovoice.patient_name=prescription.patient_name and doctor.doctor_name='$_SESSION[doctor_nm]' and inovoice.inovoice_date between '$_SESSION[from_dt]' and '$_SESSION[to_dt]' $sortingCode "; $result=mysql_query($query); while($row=mysql_fetch_array($result)) { $newdate=date("d/m/Y", strtotime($row['inovoice_date'])); ?> <tr> <td><?php echo $row['patient_name'];?></td> <td id="<?php echo $row['patient_type'];?>"><?php echo $row['patient_type'];?></td> <td id="<?php echo $row['payment_type'];?>"><?php echo $row['payment_type'];?></td> <td><?php echo $newdate;?></td> <td><?php echo $row['inovoice_amount'];?></td> </tr> <?php } ?> 

JQuery code: JQuery代码:

$( document ).ready(function() {
    $('#patient_type').change(function(){
       if($(this).val() == "InPatient")
       {
           alert("hiiiii");
           //show InPatient 
       }
    })
});

First of all you should't use id, because it is possible that there are rows with the same id. 首先,你不应该使用id,因为有可能存在具有相同id的行。 An id should always be unique. id应始终是唯一的。 The second thing is if you want to show/hide the rules you should use a identifier on rule ( tr ) level. 第二件事是如果你想显示/隐藏规则,你应该在规则( tr )级别使用标识符。 If you are using jQuery you can use a data tag, for example: 如果您使用的是jQuery,则可以使用数据标记,例如:

 <tr data-patientType="<?php echo $row['patient_type'];?>">
    <td><?php echo $row['patient_name'];?></td>
    <td><?php echo row['patient_type'];?></td>
    <td><?php echo $row['payment_type'];?></td>
    <td><?php echo $newdate;?></td>
    <td><?php echo $row['inovoice_amount'];?></td>
</tr>

In jQuery you would get something like the code below: 在jQuery中你会得到类似下面的代码:

$( document ).ready(function() {
    $('#patient_type').change(function(){

           var selectedValue = $("#patient_type option:selected").text();

           $('table tr').each(function(){

                if($(this).data('patientType') == selectedValue)
                {
                    $(this).css('display', 'block');
                }
                else
                {
                    $(this).css('display', 'hidden');
                }

           });
    })
});

您需要从选择中找到所选选项,而不是它的值。

$( "#patient_type option:selected" ).text();

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

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