jQuery'else'的问题 [英] Issue with jQuery 'else'

查看:137
本文介绍了jQuery'else'的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定自己在做什么错,但是只提取了脚本的前半部分.我尝试了各种组合,但一次只能使一个if语句起作用.

I am not sure what I'm doing wrong but only the first half of my script is being picked up. I've tried various combinations and can only get one of the if statements to work at a time.

if (!$('#LocalDelivery').is(":checked")) {
    $('#datepicker').attr("value", "");
    $('#LocalDate').val($('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').val());
    $('#LocalDate').attr('name',
    $('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').attr('name')); 
}    
else
{
if (!$('#StandardShip').is(":checked")) {
    $('#LocalDate').attr("value", "");
    $('#datepicker').val($('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').val());
    $('#datepicker').attr('name',
    $('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').attr('name'));      
}
}

谢谢.

需要澄清的是,我确实理解其他"的含义是其中之一.这里的问题是,即使第二条if语句满足条件,它也不会执行.如果仅使用上述代码或我尝试过的任何类似变体,一次只能得到一个陈述的反应.

To clarify, I do understand 'else' means either one or the other. The problem here is that even if the condition is met for the 2nd if statement, it doesn't execute. I can only get a reaction from one if statment at a time using the above code or any similar variation that I have tried.

推荐答案

尝试:

if ($('#LocalDelivery').is(":checked") == false) {
    $('#datepicker').attr("value", "");
    $('#LocalDate').val($('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').val());
    $('#LocalDate').attr('name',
    $('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').attr('name')); 
}    
if ($('#StandardShip').is(":checked") == false) {
    $('#LocalDate').attr("value", "");
    $('#datepicker').val($('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').val());
    $('#datepicker').attr('name',
    $('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').attr('name')); 

}

您也可以使用.attr("checked","checked")更改.is(":checked").

我只是注意到您正在尝试在同一元素#datepicker中实现两个特定的值,这就是导致您的第一个条件不被选择的原因.

I just noticed that you are trying to implement two specific values in the same element #datepicker and thats what must be causing your first conditional not to get picked.

我的意思是,当未选中两个复选框时,您试图将两个值放入一个元素#datepicker.

What i mean is that when both checboxes are not checked you are trying to put TWO values in ONE element #datepicker.

以上表示您的代码逻辑错误.例如,您可以仅具有一个复选框,然后执行以下操作:

The above means that the logic of your code is wrong.You could instead for example have only one checkbox and do the following:

 if ($('#LocalDelivery').is(":checked") == false) {
        $('#datepicker').attr("value", "");
        $('#LocalDate').val($('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').val());
        $('#LocalDate').attr('name',
        $('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').attr('name')); 
    } else {
        $('#LocalDate').attr("value", "");
        $('#datepicker').val($('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').val());
        $('#datepicker').attr('name',
        $('.productAttributeConfigurableEntryText div.productAttributeValue input[type="text"]').attr('name')); 

    }

这篇关于jQuery'else'的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆