jQuery Value根据条件追加并删除属性值

时间:2017-11-30 06:38:43

标签: javascript jquery html css twitter-bootstrap

我正在使用jQuery的append和attr方法,并且输入附加了输入值和bootstrap模态值。但不知怎的,我的代码没有根据条件运行我的jQuery代码

$("input").change(function(){
  var value = $(this).val();
  $("#gwrmodal").modal({backdrop: "static"}); // open modal
    $("button").click(function(){
      var gwrvalue = $(this).val();
      var id= $("#appenddiv div").length;
      $("#appenddiv div").each(function() {
        var fieldWrapper =$("<div class=\"fieldwrapper\" id=\"field" + Id + "\"/>"); //apped wrapper
        var gwrtoken = $("<input type=\"text\" id=\"" + id + "\" name=\"" + id + "\" value=\"" + value+ "\"  disabled/>"); //append token value
        var Time = $("<input type=\"text\" id=\"" + id + "\" name=\"" + id + "\" value=\"" + gwrvalue+ "\" disabled/>");//append time value
        var removeButton = $("<input type=\"button\" class=\"remove\" value=\"Remove\" />");//remove button
        removeButton.click(function() {
          $(this).parent().remove();
        });
        fieldWrapper.append(gwrtoken);
        fieldWrapper.append(gwrTime);
        fieldWrapper.append(removeButton);
        $("#appenddiv").append(fieldWrapper);
      });
      var sum= 0;
      for(var i=0;i<=id;i++){
        sum +=time;// sum of the time
        return sum;
      }
      if(sum>3){ //condition if value is greater than 3
        var removeValue =$("#input input:last").removeAttr("value");
        $("#newinput").attr("value").val(removeValue);
        return sum=0; // for next input condition
      }
    });
  });

我的jsfiddle是 https://jsfiddle.net/2snbaspw/

我想用bootstrap模态值附加输入值,如果bootstrap模态值的总和大于3,则输入值将被移除并显示在下一个div输入中。

1 个答案:

答案 0 :(得分:0)

初始化变量:

$("input").change(function(){
   var value = $(this).val();
   $("#gwrmodal").modal({backdrop: "static"}); // open modal
   $("button").click(function(){
   .........
   var time = gwrvalue; --- here
   var sum= 0; 
  for(var i=0;i<=id;i++){
    sum +=time;// sum of the time
    return sum;
  }
  if(sum>3){ //condition if value is greater than 3
    // Change Here
    $("#input input:eq("+getIndex+")").val();
            $("#newinput input:eq("+getIndex+")").val(sum);
            console.log('New Value' + $("#newinput input:eq("+getIndex+")").attr('type'))
            sum = 0; // for next input condition
  }
  });
});

检查这个最新代码: https://jsfiddle.net/2snbaspw/5/