jquery函数变量不起作用

时间:2017-09-21 22:36:47

标签: jquery

我试图让这个功能起作用,但我没有得到它。计数器不工作,我希望它成为该功能的第一个条件。我也无法达到第二个条件。它始终是第一个条件。

这是jquery代码:

var cpcounter1 = 0;
var cpcounter2 = 0;
var inpreco = ["", ""];
var inprocess = ["", ""];

$(".opcaopreco1").click(function () {
    Arra(this, "#preco", inpreco, "cpindex1" ,"cpactive1", cpcounter1, "preco");
});
$(".opcaopreco2").click(function () {
    Arra(this, "#process", inprocess, "cpindex2", "cpactive2", cpcounter2, "process");
});
function Arra(element, input, inpArray, secIndex, inpActive, counter, msqlip) {
    var inpValue = $("#" + element.id).val();      
    var inpActive = $("#" + element.id).data(inpActive);
    var secIndex = $("#" + element.id).data(secIndex);

    if (counter==0){
        inpArray[0] = (inpValue);
        $("#z1").html("result1");  
    }else
    if (inpActive=="") {
        inpArray[secIndex]=(" OR "+msqlip+" BETWEEN "+inpValue);        
        $("#z1").html("result2");                  
    }
    counter++;
    $(input).val(inpArray[0]+inpArray[1]);   
    $("#z2").html(counter);       
};

这是html代码:

<input id="preco" type="text" name="preco" value='1 AND 5000'><br><br>
    <input id="process" type="text" name="process" value='1 AND 11'><br><br>
    <div id="op1l" class="input">
        <input type="checkbox" id="op1" class="opcaopreco1" value="Start" data-cpindex1="1" data-cpactive1="Test1">
        <label for="op1"></label>
        <span class="itext">Test1</span>
    </div>      
    <div id="op2l" class="input">
        <input type="checkbox" id="op2" class="opcaopreco1" value="Start2" data-cpindex1="2" data-cpactive1="Test2">
        <label for="op2"></label>
        <span class="itext">Test2</span>
    </div>
    <div id="op3l" class="input">
        <input type="checkbox" id="op3" class="opcaopreco2" value="Start3" data-cpindex2="1" data-cpactive2="Test3">
        <label for="op3"></label>
        <span class="itext">Test1</span>
    </div>      
    <div id="op4l" class="input">
        <input type="checkbox" id="op4" class="opcaopreco2" value="Start4" data-cpindex2="2" data-cpactive2="Test4">
        <label for="op4"></label>
        <span class="itext">Test2</span>
    </div>
    <ul id="z">
        <li id="z1">z1</li>         
        <li id="z2">z2</li>
    </ul>

1 个答案:

答案 0 :(得分:0)

您可以使用解决方案https://jsfiddle.net/ew2r3ytb/

&#13;
&#13;
var cpcounter1 = 0;
var cpcounter2 = 0;
var inpreco = ["", ""];
var inprocess = ["", ""];

$(".opcaopreco1").click(function () {
    cpcounter1 = Arra(this, "#preco", inpreco, "cpindex1" ,"cpactive1", cpcounter1, "preco");
});
$(".opcaopreco2").click(function () {
    cpcounter2 = Arra(this, "#process", inprocess, "cpindex2", "cpactive2", cpcounter2, "process");
});
function Arra(element, input, inpArray, secIndex, inpActive, counter, msqlip) {
  var inpValue = $("#" + element.id).val();      
  var inpActive = $("#" + element.id).data(inpActive);
  var secIndex = $("#" + element.id).data(secIndex);
  if (counter == 0){
    inpArray[0] = (inpValue);
    $("#z1").html("result1");  
  }else if (inpActive=="") {
    inpArray[secIndex]=(" OR "+msqlip+" BETWEEN "+inpValue);        
    $("#z1").html("result2");                  
  }
  counter++;
  $(input).val(inpArray[0]+inpArray[1]);   
  $("#z2").html(counter);       
  return counter;
};
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="preco" type="text" name="preco" value='1 AND 5000'><br><br>
<input id="process" type="text" name="process" value='1 AND 11'><br><br>
<div id="op1l" class="input">
    <input type="checkbox" id="op1" class="opcaopreco1" value="Start" data-cpindex1="1" data-cpactive1="Test1">
    <label for="op1"></label>
    <span class="itext">Test1</span>
</div>      
<div id="op2l" class="input">
    <input type="checkbox" id="op2" class="opcaopreco1" value="Start2" data-cpindex1="2" data-cpactive1="Test2">
    <label for="op2"></label>
    <span class="itext">Test2</span>
</div>
<div id="op3l" class="input">
    <input type="checkbox" id="op3" class="opcaopreco2" value="Start3" data-cpindex2="1" data-cpactive2="Test3">
    <label for="op3"></label>
    <span class="itext">Test1</span>
</div>      
<div id="op4l" class="input">
    <input type="checkbox" id="op4" class="opcaopreco2" value="Start4" data-cpindex2="2" data-cpactive2="Test4">
    <label for="op4"></label>
    <span class="itext">Test2</span>
</div>
<ul id="z">
    <li id="z1">z1</li>         
    <li id="z2">z2</li>
</ul>
&#13;
&#13;
&#13;

您需要返回计数器值&amp;捕获您调用Arra方法的值。

希望这会对你有所帮助。