【问题标题】:Code will execute both if and else statements. Javascript [closed]代码将同时执行 if 和 else 语句。 Javascript [关闭]
【发布时间】:2015-02-26 00:54:50
【问题描述】:

我有一个名为 used 的数组。我有一个在数组中添加代码的函数,但会检查代码是否已再次添加。如果已添加,则必须说“假”否则为真 问题是它会说假和真。 这是我的代码:

   var used=[];//use that to check if the user has added the code to favourites again

var x='',y='',blur='',r="",g="",b="",op="",color="rgb(0,0,0)",shadow=''; //create the variables that will be used 
function update(){
    x=$("#x").val();
    y=$("#y").val();
    op=$("#o").val();
    r=$("#red").val();
    g=$("#green").val();
    b=$("#blue").val();
    blur=$("#blur").val();
    shadow = x+"px "+y+"px ";


}
$(document).ready(function(){
 $("#addC").click(function(){
  if(used.indexOf(shadow) != -1){
    alert("false");
  }
  else{
    used.push(shadow);
    alert("True");

  }
 });
   $('.shadow-slider').change(function(){

    update();

     if (op != 1){
    if(op.length > 3){op=op.substring(0, 3);}
    color="rgba("+r+","+g+","+b+","+op+")";
   }
   else{
    color="rgb("+r+","+g+","+b+")";
   }
   if (blur != 0){shadow += blur+"px ";}
   shadow += color;
   $('#object').css("text-shadow",shadow);
   $('#code-output').html("text-shadow:"+shadow+";");

   });



});

Shadow 是一个全局变量,它会根据我页面的某些滑块而变化。有什么想法吗?

【问题讨论】:

  • 您的点击事件处理程序被触发了两次。
  • 在哪里触发了两次。我什么都没看到

标签: javascript html if-statement conditional-statements


【解决方案1】:

我认为你已经绑定了两次点击处理程序,所以第一次没有找到它,你得到了第二种情况(并添加它)但第二次找到了(第一次添加)并显示第一个案例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多