jQuery’else’的问题

我不确定我做错了什么,但只是我的脚本的前半部分被接收了。 我尝试了各种组合,并且一次只能获得一个if语句。

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语句满足条件,它也不会执行。 我只能使用上面的代码或我尝试的任何类似变体,一次得到一个人的反应。

试试:

 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") .attr("checked","checked")

编辑:我刚刚注意到你正在尝试在同一个元素#datepicker实现两个特定的值,这就是必须导致你的第一个条件不被选中的原因。

我的意思是,当没有检查两个checbox时,你试图将两个值放在一个元素#datepicker

上面的意思是代码的逻辑是错误的。例如,您可以只有一个复选框,并执行以下操作:

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

尝试

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

如果你正在使用else, if如果第一个if将返回false ,那么将执行第二个if ,所以你拥有的是你应该期望的。 第一个if返回true,所以忽略第二个。

这就是应该如何运作的。 如果前半部分为true ,则不执行else块。 每次只执行一个块。 从来没有

这就是ifelse意思。

 if (A) { B } else { C } 

如果是A ,则B否则为 C

如果你不打算这个小方程的“否则”部分,那么不要使用else