2017-09-24 76 views
0

我一直在研究一個函數,它會根據選擇的選項給出不同的輸入結果。但是,艾滋病使它無法正常工作。我需要在這個功能中使用「計數器」,但我一直無法正常工作。任何人都可以提供解決方案或提示如何使此代碼工作?jquery函數參數計數器不能正常工作

這是jQuery代碼:

function test90(element, input, inpArray, secIndex, inpActive, counter, Ipp) { 
       Ipp = $("#" + element.id).data(inpActive); 
       if (counter == 0){ 
        counter++; 
        $(element).data(inpActive, "primary"); 
        inpArray[0]=("result1"); 
       }else 
       if (Ipp=="no") { 
        counter++; 
        $(element).data(inpActive, "yes"); 
        inpArray[secIndex]="result2";     
       }else 
       if (Ipp=="yes") { 
        counter--; 
        $(element).data(inpActive, "no");    
        inpArray[secIndex]=""; 
       }else 
       if (Ipp=="primary" && counter!==1) { 
        counter--; 
        $(element).data(inpActive, "no"); 
        inpArray[0]="result3";  
       }else 
       if (Ipp=="primary" && counter==1) { 
        counter--; 
        $(element).data(inpActive, "no"); 
        inpArray[secIndex]=""; 
        inpArray[0]="result4";  
       } 
       $(input).val(inpArray[0]+inpArray[1]+inpArray[2]); 
       $("#z1").html("counter = "+counter); 

       $("#z2").html("Ipp = "+Ipp);     
      }; 
      var inpreco = ["", "", ""]; 
      var inprocess = ["", "", ""]; 
      var cpcounter1 = 0; 
      var cpcounter2 = 0; 

      $(".opcaopreco1").click(function() { 
       cpcounter1=test90(this, "#preco", inpreco, "cpindex1", "cpactive1", cpcounter1, "Ipp1"); 
      }); 
      $(".opcaopreco2").click(function() { 
       cpcounter2=test90(this, "#process", inprocess, "cpindex2", "cpactive2", cpcounter2, "Ipp2"); 
      }); 

這是html代碼:

<input id="preco" type="text" name="preco" value='preco'><br><br> 
    <input id="process" type="text" name="process" value='process'><br><br> 
<div id="op1l" class="input"> 
    <input type="checkbox" id="op1" class="opcaopreco1" value="Start1" data-cpindex1="1" data-cpactive1="no"> 
    <label for="op1"></label> 
    <span class="itext">preco1</span> 
</div>  
<div id="op2l" class="input"> 
    <input type="checkbox" id="op2" class="opcaopreco1" value="Start2" data-cpindex1="2" data-cpactive1="no"> 
    <label for="op2"></label> 
    <span class="itext">preco2</span> 
</div> 
<div id="op3l" class="input"> 
    <input type="checkbox" id="op3" class="opcaopreco1" value="Start3" data-cpindex1="3" data-cpactive1="no"> 
    <label for="op3"></label> 
    <span class="itext">preco3</span> 
</div> 
<br> 
<div id="op4l" class="input"> 
    <input type="checkbox" id="op4" class="opcaopreco2" value="Start1" data-cpindex2="1" data-cpactive2="no"> 
    <label for="op4"></label> 
    <span class="itext">process1</span> 
</div>  
<div id="op5l" class="input"> 
    <input type="checkbox" id="op5" class="opcaopreco2" value="Start2" data-cpindex2="2" data-cpactive2="no"> 
    <label for="op5"></label> 
    <span class="itext">process2</span> 
</div> 
<div id="op6l" class="input"> 
    <input type="checkbox" id="op6" class="opcaopreco2" value="Start3" data-cpindex2="3" data-cpactive2="no"> 
    <label for="op6"></label> 
    <span class="itext">process3</span> 
</div> 
<ul id="z"> 
    <li id="z1">z1</li>   
    <li id="z2">z2</li> 
</ul> 

回答

1

你分配由test90的價值迴歸,但功能不返回任何值。所以你應該從函數返回計數器。否則,未定義的值將從函數返回。 試試這個,

function test90(element, input, inpArray, secIndex, inpActive, counter, Ipp) { 
       Ipp = $("#" + element.id).data(inpActive); 
       if (counter == 0){ 
        counter++; 
        $(element).data(inpActive, "primary"); 
        inpArray[0]=("result1"); 
       }else 
       if (Ipp=="no") { 
        counter++; 
        $(element).data(inpActive, "yes"); 
        inpArray[secIndex]="result2";     
       }else 
       if (Ipp=="yes") { 
        counter--; 
        $(element).data(inpActive, "no");    
        inpArray[secIndex]=""; 
       }else 
       if (Ipp=="primary" && counter!==1) { 
        counter--; 
        $(element).data(inpActive, "no"); 
        inpArray[0]="result3";  
       }else 
       if (Ipp=="primary" && counter==1) { 
        counter--; 
        $(element).data(inpActive, "no"); 
        inpArray[secIndex]=""; 
        inpArray[0]="result4";  
       } 
       $(input).val(inpArray[0]+inpArray[1]+inpArray[2]); 
       $("#z1").html("counter = "+counter); 

       $("#z2").html("Ipp = "+Ipp); 

       return counter; /* ADD RETURN LIKE THIS */ 
      }; 
      var inpreco = ["", "", ""]; 
      var inprocess = ["", "", ""]; 
      var cpcounter1 = 0; 
      var cpcounter2 = 0; 

      $(".opcaopreco1").click(function() { 
       cpcounter1=test90(this, "#preco", inpreco, "cpindex1", "cpactive1", cpcounter1, "Ipp1"); 
      }); 
      $(".opcaopreco2").click(function() { 
       cpcounter2=test90(this, "#process", inprocess, "cpindex2", "cpactive2", cpcounter2, "Ipp2"); 
      }); 
+0

它解決了這個問題。謝謝 – Adato