dtyrxmoj20617 2019-02-02 14:45
浏览 60
已采纳

苦苦挣扎到POSTJavascript

I have a few inputs that I am taking the values from and displaying them as a Cash_total. I am wanting to get the cash_total into settings.php and have tried allsorts but just can't figure it out. I've got all the input values there with this:

    submitHandler: function(form) {
  $.ajax({
    url: "includes/settings.php",
    type: "POST",
    data: $('form').serialize(),
    success: function(strMessage) {
      $('#message').text(strMessage)
    }
  });
  return false;
}

Here is the rest of what I have got

$('.cashinput').keyup(function() {


  var sum = 0;
  $('.cashinput').each(function() {
    sum += Number($(this).val());
  });


  $('#cash_total').val(sum);
});
input,
select {
  background-color: #FFFFFF;
  border: solid 1px #A9A9A9;
  font-size: 13px;
  color: #000000;
  -moz-border-radius: 7px;
  -webkit-border-radius: 7px;
  border-radius: 7px;
  padding-top: 5px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  display: block;
  margin-bottom: 5px;
}

label {
  font-size: 14px;
  color: #333333;
  font-weight: normal;
  padding-top: 0px;
  padding-bottom: 0px;
  float: left;
  width: 77px;
  text-align: right;
  margin-right: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="cash_up" method="POST">
<label for="cash_two_pence">2p </label><input id="cash_two_pence" type="number" step="0.02" name="cash_two_pence" class="cashinput" placeholder="0.00" />
<label for="cash_one_pence">1p </label><input id="cash_one_pence" type="number" step="0.01" name="cash_one_pence" class="cashinput" placeholder="0.00" />

<label for="cash_total">Cash Total</label><input type='text'     name='cash_total' id='cash_total' disabled />
</input type="submit" value="Next" id="save_button"/>
</form>

Please could someone point me at the solution. :-)

</div>
  • 写回答

1条回答 默认 最新

  • dtrz17917 2019-02-02 15:08
    关注

    You need form tags, a submit button and the handler attached to the form submit event which you then prevent

    You cannot submit a disabled field. Change to readonly

    $(function() {
      $('.cashinput').on("input", function() {
        var sum = 0;
        $('.cashinput').each(function() {
          sum += Number($(this).val());
        });
        $('#cash_total').val(sum);
      });
    
      $("form").on("submit", function(e) {
        e.preventDefault(); //stop submission
        $.ajax({
          url: "includes/settings.php",
          type: "POST",
          data: $(this).serialize(),
          success: function(strMessage) {
            $('#message').text(strMessage)
          }
        });
      });
    });
    input,
    select {
      background-color: #FFFFFF;
      border: solid 1px #A9A9A9;
      font-size: 13px;
      color: #000000;
      -moz-border-radius: 7px;
      -webkit-border-radius: 7px;
      border-radius: 7px;
      padding-top: 5px;
      padding-bottom: 5px;
      padding-left: 5px;
      padding-right: 5px;
      display: block;
      margin-bottom: 5px;
    }
    
    label {
      font-size: 14px;
      color: #333333;
      font-weight: normal;
      padding-top: 0px;
      padding-bottom: 0px;
      float: left;
      width: 77px;
      text-align: right;
      margin-right: 10px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <form id="myform">
      <label for="cash_two_pence">2p </label><input id="cash_two_pence" type="number" step="0.02" name="cash_two_pence" class="cashinput" placeholder="0.00" />
      <label for="cash_one_pence">1p </label><input id="cash_one_pence" type="number" step="0.01" name="cash_one_pence" class="cashinput" placeholder="0.00" />
    
      <label for="cash_total">Cash Total</label><input type='text' name='cash_total' id='cash_total' readonly />
      <input type="submit" />
      <span id="message"></span>
    </form>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)