Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

jquery - I have a form and i want to add all the is not equal to zero

This This is my desired output. I want the date will be equals to 1 and add in the total input and in class there's an additional 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, as I understand, you have different input fields and you want only those that have values != 0. You can use this pseudo code which traverses thru all input fields and your code will execute only if 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...