Jquery 需要根据下拉选择更改位于“提交”按钮上方的消息

Jquery 需要根据下拉选择更改位于“提交”按钮上方的消息,jquery,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('.sel

我一直在努力让这段代码在工作中运行大约一个小时,并且一直在努力寻找它不能按我的预期运行的原因。我希望也许另一双眼睛能让我的一天充满活力

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('');
    }
});

链接:

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

$(document).ready(function(){
    jQuery('select[name="form[State]"]').change(function() {
        //your function 
    });
});
另一种方法是使用
on()


如果您使用的是Internet Explorer,那么您的逻辑看起来是正确的。你能不能也发布html来找出问题所在?如果你转到问题底部的链接,它就是页面顶部的表单。它似乎工作正常。我将您的代码放在控制台中,当我将该州更改为“德克萨斯州”时,我看到“您所在州的一家获得许可的信贷服务机构将尝试代表您获得分期付款贷款。请单击此处确认并继续。”尽管如此,其黑色文本显示为深灰色。很难看到。此外,表单更改是因为select消息中的HTML变得非常大,但这应该很容易修复。这就是wierd,也许我们的网络正在缓存snappypaydayloans.com?你在我链接的网站上查过了?当我访问该网站时,它不起作用。我只是添加了它,但它似乎仍然不起作用。@StephenSabatini你能试试我的第二个建议吗?很好-起作用了。我想第一个不起作用,因为我忘了在dreamweaver中推它,只是保存了它。漫长的一天-非常感谢@史蒂芬萨巴蒂尼也认为史蒂夫的建议很好。您可以简单地使用
this.value
而不是
jQuery(this.val()
,使代码看起来更简洁。我们已经开始停止对IE8的支持,但这不是问题所在,因为我在Windows 7上使用的是最新版本的Chrome和Firefox。
$(document).ready(function(){
    jQuery(document).on('change', 'select[name="form[State]"]', function() {
        //your function 
    });
});
if (jQuery.inArray(jQuery(this).val(), ['CT','GA','NY','WA','AK','DC','MA','MT','NH','VT','WV']) > -1)