2012-10-31 116 views
1

我正在使用jQuery的onchange event of select box。 在更改时,我在其他div中给出了display property = none。 问题是,当我从数据库获取数据并将其设置在选择框中时,onchange属性未应用于给定值。为什么? 我的代码Onchange事件选择框

<select id="patientType" name="Patient_Type"> 
<option value="">--SELECT--</option> 
<option value="OPD">OPD</option> 
<option value="IPD">IPD</option> 
</select> 
<div id="roomInfo" clasas="inactive"></div> 

脚本

$('#patientType').change(function() { 
if ($('#patientType').val() == 'IPD') { 
    if ($('#roomInfo').hasClass('inactive')) { 
    $('#roomInfo').removeClass('inactive');// CSS Property Display None in this class 
    $('#roomInfo').addClass('active'); 
    } 
} else { 
    if ($('#roomInfo').hasClass('active')) { 
    $('#roomInfo').removeClass('active'); 
    $('#roomInfo').addClass('inactive'); 
    } 
} 
}); 

现在,我得到的数据从数据库并设置该选择patientType $( '#patientType')VAL(数据从数据库中获取)。 不能申请onchange事件适用,不能显示我的roomInfo div 所以有可能与否。 或另一种获取该方法的方法。 请帮忙。 提前致谢。

+1

请向我们显示您的代码 –

+0

如何从数据库中提取数据?它是ajax还是页面加载? –

回答

1

获取选择价值盒子,并像这样设置:

$("#patientType option[value='`OPD']").attr("selected","selected"); 
5

由于您手动更改了值。您需要触发选择框的.change()事件。 请参阅以下链接。

http://api.jquery.com/change/

否则

如果您加载通过Ajax使用的数据,如以下

$('#patientType').on("change",function() { 
    //Your code here 
}); 
+0

“从jQuery 1.7开始,'.live()'方法不再使用,使用'.on()'附加事件处理程序。” - http://api.jquery.com/live/ –

3

jQuery中设置的更改事件:

$("#ddl").change(function(){ 
    alert($(this).val()); 
});