需要在提交按钮上方的消息根据下拉选择进行更改

时间:2016-03-02 21:44:47

标签: jquery

我一直试图让这段代码在工作中工作大约一个小时,并且一直在努力找到它无法正常工作的原因。我希望也许另一双眼睛可以活跃我的一天。

jQuery('select[name="form[State]"]').change(function() {
    if ( ['CT','GA','NY','WA','AK','DC','MA','MT','NH','VT','WV'].indexOf(jQuery(this).val()) >= 0) {
        jQuery('.select-message').html('Unfortunately at this time, due to state regulation, we are unable to present a loan offer to you while you reside in your state. We apologize for any inconvenience this may have caused');
    } else if ( jQuery(this).val() == 'IL') {
        jQuery('.select-message').html('The Direct Lender licensed in your state offers an open ended revolving credit plan. Please click here to acknowledge and proceed.');
    } else if ( ['WI','MO','NM'].indexOf(jQuery(this).val()) >= 0) {
        jQuery('.select-message').html('The Direct Lender licensed in your state offers an installment loan. Please click here to acknowledge and proceed.');
    } else if ( jQuery(this).val() == 'TX') {
        jQuery('.select-message').html('A Credit Service Organization licensed in your state will attempt to obtain an installment loan on your behalf. Please click here to acknowledge and proceed.');
    } else {
        jQuery('.select-message').html('');
    }
});

链接:https://www.snappypaydayloans.com/

2 个答案:

答案 0 :(得分:2)

将代码移到$(document).ready(function(){})中,因为在加载标记之前添加了事件处理程序。

$(document).ready(function(){
    jQuery('select[name="form[State]"]').change(function() {
        //your function 
    });
});

另一种方法是使用on()

$(document).ready(function(){
    jQuery(document).on('change', 'select[name="form[State]"]', function() {
        //your function 
    });
});

答案 1 :(得分:0)

如果您使用的是Internet Explorer< = 8,则indexOf无法在阵列上运行。请改用jQuery.inArray

所以将你的第一个if语句改为:

if (jQuery.inArray(jQuery(this).val(), ['CT','GA','NY','WA','AK','DC','MA','MT','NH','VT','WV']) > -1)