我有一个表单,我想添加所有不等于零

时间:2017-07-27 06:34:21

标签: jquery twitter-bootstrap laravel-5.4

This这是我想要的输出。我希望date等于1并添加总输入,并且在课堂上还有一个额外的x。

<div class="form-group{{ $errors->has('day1') ? ' has-error' : '' }}">
                        <label for="day1" class="col-md-4 control-label">Day 1</label>

                        <div class="col-md-6">
                           <div class='input-group date' id='day1'>
                              <input type='text' class="form-control" name="day1" value="0" readonly="readonly" />
                                <span class="input-group-addon">
                                    <span class="glyphicon glyphicon-calendar">
                                    </span>
                                </span>
                            </div>
                        </div>
                    </div>

enter image description here

1 个答案:

答案 0 :(得分:0)

嗯,据我所知,您有不同的input字段,而您只想拥有values != 0的字段。您可以使用遍历所有input字段的伪代码,只有在value != 0时才会执行代码:

$('input').each(function() {
  if ($(this).val() != 0) {
    // code goes here...
  }
});

$('input').on('change', function() {
  updateCount();
})

function updateCount() {
  var count = 0;
  $("input:not('.k-textbox')").each(function() {
    if ($(this).val() != 0) {
      count++;
    }
  });
  $(".k-textbox").val(count + 'x');
  console.log(count + 'x');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
  <tr>
    <td>
      <input type='text' name='total[]' id='total1' value='0' />
    </td>
  </tr>

  <tr>
    <td>
      <input type='text' name='total[]' id='total2' value='0' />
    </td>
  </tr>


  <tr>
    <td>
      <input type='text' name='total[]' id='total3' value='0' />
    </td>
  </tr>


  <tr>
    <td>
      <input type='text' name='total[]' id='total4' value='0' />
    </td>
  </tr>



  <tr>
    <td>
      <input type="text" class="k-textbox" value="" style="color: red; text-align: right; font-family: courier" name="total_amt_due" id="amt_due" readonly="readonly" />
    </td>
  </tr>
</table>