简体   繁体   English

选择选项并隐藏div jquery

[英]select option and hide div jquery

I've been searching without luck a solution to this...Im trying to do a function that hide a div on an selected option in a select box but I cant make it work properly, I mean, it works if I refresh the page with the option that hide the div selected, the div is hided, I change option, the div is visible, BUT if I come back to the option that should hide the div... it doesnt hide it... What Im doing wrong? 我一直在寻找解决方案,但运气不佳...我正在尝试执行一个将div隐藏在选择框中的选定选项上的功能,但我无法使其正常运行,我的意思是,如果刷新页面则可以正常工作用隐藏所选div的选项,隐藏div,更改选项,显示div,但是如果我回到应该隐藏div的选项...它没有隐藏它...我在做什么错?

This is my function: 这是我的功能:

  $(function(){ if ( $('select#CW_Type').val() ==='APPROVED' ) { $('#creditLimit').addClass('hide'); $('select#CW_Type').on("change",function() { $('#creditLimit').removeClass('hide'); }); }; }); 

the id "#creditLimit" is the div that should be hided and then be showed again This is my HTML id“ #creditLimit”是应该隐藏然后再次显示的div。这是我的HTML

 <div> <label> Type <select id="CW_Type" name="CW_Type"> <option value="-">-</option> <option value="APPROVED">APPROVED</option> <option value="LIMIT">LIMIT</option> <option value="MANUAL_DECISION">MANUAL DECISION</option> <option class="selected" value="DENIED">DENIED</option> </select> </label> </div> <div id="creditLimit" class="span6"> <label class="half"> Credit Limit <input type="number" name="CW_CreditLimit" value="0" min="0" step="1" data-validate="currency"> </label> <label class="half"> Currency <select name="CW_Currency"> <option value="-">-</option> <option value="SEK">SEK</option> <option value="EUR">EUR</option> </select> </label> </div> <style> .hide {display:none;} </style> 
Thanks in advance for any suggestion! 预先感谢您的任何建议!

You need a change function for the select , then check the value and do your logic: 您需要为select提供change功能,然后检查该值并执行逻辑:

$("select#CW_Type").change(function() {
    if ( this.value ==='APPROVED' ) {
        $('#creditLimit').addClass('hide');
    } else {
        $('#creditLimit').removeClass('hide');
    }
}).change(); //call on load

You currently have a change handler - but it's only being bound when your if condition is satisfied - and if that isn't on load, then the handler will never be bound. 您目前有一个change处理程序-但是只有在满足if条件时才进行绑定-并且如果该change未加载,则该处理程序将永远不会被绑定。

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

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