简体   繁体   English

if-else条件无法在Javascript中正确执行

[英]If-else condition not executing correctly in Javascript

I hope the question is not too silly, but I am stuck with it and have tried may things. 我希望这个问题不要太愚蠢,但我会坚持下去,并尝试过可能的事情。 I have a dropdown in a table row which I am adding dynamically, and I am associating a change event with the dropdown. 我在要动态添加的表行中有一个下拉列表,并且正在将change事件与该下拉列表相关联。 I have the following code here: 我在这里有以下代码:

$(document).on("change", $('tr').find('select'), function(){

    if ($(this).val() == 0){
        $('#items_selected').text(1);
        alert("2nd time wrong");
    }else{
        alert("2nd time right");
        $('#items_selected').text(parseInt($('#items_selected').text()) + parseInt($(this).val()));
    }
});

Now, there is a checkbox, which when checked causes the dropdown (previously disabled) to become enabled.This fires the change event properly, executing the if condition in the code above. 现在,有一个复选框,选中该复选框会导致下拉列表(以前被禁用)被启用,这会正确触发change事件,并执行上面代码中的if条件。 But when I change the value in the dropdown from the default value of 0 to something else, the event does fire, but again the if condition executes, not the else, which should now work. 但是,当我将下拉列表中的值从默认值0更改为其他值时,该事件会触发,但是再次执行if条件,而不是else,现在应该可以了。 Is there something wrong with my event type ( change ), or are my datatypes (string/number) messed up, or is it something else? 我的事件类型( change )是否有问题,或者我的数据类型(字符串/数字)是否混乱,还是其他原因? I tried parseInt() and other things but they didn't work. 我尝试了parseInt()和其他方法,但是它们没有起作用。 Can someone help please? 有人可以帮忙吗?

You should check the select text value like $(this).find("option:selected").val() 您应该检查选择文本值,例如$(this).find("option:selected").val()

Below code works: 下面的代码有效:

$(document).on("change", $('tr').find('select'), function(){    
    if ($(this).find("option:selected").val() == 0){   
        alert($(this).find("option:selected").val());
    }else{
        alert($(this).find("option:selected").val());         
    }
});

Working FIDDLE 工作场所

In the present code you call on on $(document) . 在本次代码调用on$(document) As a result, in the event handler, this will be the document (even if you are actually looking at events happening on the dropdown). 结果,在事件处理程序中, this将是document (即使您实际上正在查看下拉列表中发生的事件)。

You can try something like this: 您可以尝试如下操作:

 $("#the-select").on("change", function(){ // good practice: always use "===" if ($(this).val() === "0"){ $('#items_selected').text(1); alert("2nd time wrong"); }else{ alert("2nd time right"); $('#items_selected').text(parseInt($('#items_selected').text()) + parseInt($(this).val())); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="the-select"> <option>0</option> <option>1</option> <option>2</option> <option>3</option> </select> <div id="items_selected">0</div> 

If you absolutely want to call on on $(document) (though I can't see why you would), then keep in mind that this won't be your dropdown. 如果你绝对要叫on$(document)虽然我不明白为什么你会),那么请记住, this不会是你的下拉列表。 Put it in a variable in the upper scope, re-select it in the handler, or bind your handler to the dropdown. 将其放在较高范围的变量中,在处理程序中重新选择它,或bind您的处理程序bind到下拉列表。

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

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