当显示从无到阻止时,动态表单不提交

时间:2017-04-25 09:56:04

标签: javascript jquery html forms

我创建了一个脚本,该脚本发送一个表单,该表单根据用户的选择发送一个动态表单。

html端的表单看起来很好,jQuery端的代码执行正常,直到实际的表单提交,控制台日志中没有任何内容告诉我有任何错误。

我唯一能想到的是这个表格开始显示:无;在css中然后变为可用的人点击按钮说添加新的付款。

这是html方面的事情:

<div class="section-9">

  <form action="#" id="addform" method="post">
    <div class="row">

      <div class="col-sm-12">

        <div class="table-responsive" id="addsection">

          <table class="table table-responsive table-hover table-striped">

            <thead>
              <th>Number</th>
              <th>Price</th>
              <th class="text-center">Installments</th>
              <th>Contact Name</th>
            </thead>
            <tbody>
              <tr>
                <td><input type="text" class="form-control" id="addnumber" value="" placeholder="Enter CPO Number"></td>
                <td><input type="text" class="form-control" id="addprice" value="" placeholder="Enter CPO Number"></td>
                <td class="text-center"><a href="#" class="btn btn-danger addi">Installments</a></td>
                <td><input type="text" class="form-control" id="addcontactname" value="" placeholder="Enter Contact Name"></td>
              </tr>
            </tbody>

          </table>

        </div>

      </div>

      <div class="col-sm-12" id="addformajax"></div>

      <div class="col-sm-12 margin-top-15">

        <p><button class="btn btn-danger btn-block" type="button">SUBMIT</button></p>

      </div>

    </div>
  </form>

</div>

无需在第9节中显示css作为唯一的显示。

$('#addnew').on('click', function(e) {

      e.preventDefault();

      $('.section-9').show();

      //do the click button for cpo installments
      $('.addi').on('click', function(event) {

        event.preventDefault();

        var installmentAmount = '<p><select class="form-control" id="installment-ammount"><option value="0">Please Select How Many Installments Are Required</option>';

        for (var i = 1; i <= 60; i++) {

          if (i === 1) {
            installmentAmount += '<option value="' + i + '">' + i + ' Month</option>';
          } else {
            installmentAmount += '<option value="' + i + '">' + i + ' Months</option>';
          }

        }

        installmentAmount += '</select></p><div class="showinstallmentdates margin-top-20"></div>';

        $('#addformajax').html(installmentAmount);

        $('#installment-ammount').bind('input', function() {

          var buildDateForms = '<p class="red padding-top-20"><i class="fa fa-star"></i> <em>If all amounts are left empty the price will be distributed evenly across all dates</em></p>';
          var howManyInstallments = $(this).val();
          var addingIdNames = '';

          for (var hmi = 1; hmi <= howManyInstallments; hmi++) {

            buildDateForms += '<div class="form-group row"><div class="col-xs-6"><input type="text" class="form-control" id="adddate-' + hmi + '" placeholder="Enter Date To Be Paid" value=""></div><div class="col-xs-6"><input type="text" class="form-control" id="addprice-' + hmi + '" placeholder="Amount To Be Paid" value=""></div></div>';
            if (hmi == 1) {
              addingIdNames += '#adddate-' + hmi;
            } else {
              addingIdNames += ', #adddate-' + hmi;
            }

          }

          buildDateForms += '<input type="hidden" value="' + howManyInstallments + '" name="totalinstallments" id="totalinstallments">';
          buildDateForms += '<script>jQuery(document).ready(function($){ $("';
          buildDateForms += addingIdNames;
          buildDateForms += '").datepicker({});});<\/script>';

          if (howManyInstallments != 0) {
            $('.showinstallmentdates').html(buildDateForms);
          } else {
            $('.showinstallmentdates').html('');
          }

        });

      });

      $("#addform").on('submit', function() {
        $.ajax({
          url: "/Applications/Controllers/Quotes/ajax-add-sin.php",
          type: "POST",
          data: new FormData(this),
          contentType: false,
          cache: false,
          processData: false,
          success: function(sinData) {

            $('body').html(sinData);

          }
        });

      });

    });

当然,我对jQuery并不惊讶,因为它不是我经常使用的东西,我相信一个wiz能够把它砍下来以提高效率和流线型但是根据控制台我没有问题,而且html当它全部显示时看起来也很好,所以我看不出表格未提交的原因。

由于

1 个答案:

答案 0 :(得分:0)

将ID添加到按钮

<button id="btn-add-form" class="btn btn-danger btn-block" type="button">SUBMIT</button>

将脚本放入document.ready function

将ajax功能更改为

$("#btn-add-form").on('click', function () {
    $.ajax({
        url: "/Applications/Controllers/Quotes/ajax-add-sin.php",
        type: "POST",
        data: new FormData(this),
        contentType: false,
        cache: false,
        processData: false,
        success: function (sinData) {
            $('body').html(sinData);
        }
    });
});

完整代码:

  <html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title>New Page 1</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#addnew').on('click', function (e) {
                    e.preventDefault();
                    $('.section-9').show();
                    //do the click button for cpo installments
                    $('.addi').on('click', function (event) {

                        event.preventDefault();
                        var installmentAmount = '<p><select class="form-control" id="installment-ammount"><option value="0">Please Select How Many Installments Are Required</option>';
                        for (var i = 1; i <= 60; i++) {

                            if (i === 1) {
                                installmentAmount += '<option value="' + i + '">' + i + ' Month</option>';
                            } else {
                                installmentAmount += '<option value="' + i + '">' + i + ' Months</option>';
                            }

                        }

                        installmentAmount += '</select></p><div class="showinstallmentdates margin-top-20"></div>';
                        $('#addformajax').html(installmentAmount);
                        $('#installment-ammount').bind('input', function () {

                            var buildDateForms = '<p class="red padding-top-20"><i class="fa fa-star"></i> <em>If all amounts are left empty the price will be distributed evenly across all dates</em></p>';
                            var howManyInstallments = $(this).val();
                            var addingIdNames = '';
                            for (var hmi = 1; hmi <= howManyInstallments; hmi++) {

                                buildDateForms += '<div class="form-group row"><div class="col-xs-6"><input type="text" class="form-control" id="adddate-' + hmi + '" placeholder="Enter Date To Be Paid" value=""></div><div class="col-xs-6"><input type="text" class="form-control" id="addprice-' + hmi + '" placeholder="Amount To Be Paid" value=""></div></div>';
                                if (hmi == 1) {
                                    addingIdNames += '#adddate-' + hmi;
                                } else {
                                    addingIdNames += ', #adddate-' + hmi;
                                }

                            }

                            buildDateForms += '<input type="hidden" value="' + howManyInstallments + '" name="totalinstallments" id="totalinstallments">';
                            buildDateForms += '<script>jQuery(document).ready(function($){ $("';
                            buildDateForms += addingIdNames;
                            buildDateForms += '").datepicker({});});<\/script>';
                            if (howManyInstallments != 0) {
                                $('.showinstallmentdates').html(buildDateForms);
                            } else {
                                $('.showinstallmentdates').html('');
                            }

                        });
                    });
                });
$("#btn-add-form").on('click', function () {
    $.ajax({
        url: "/Applications/Controllers/Quotes/ajax-add-sin.php",
        type: "POST",
        data: $('#addform').serialize(),
        contentType: false,
        cache: false,
        processData: false,
        success: function (sinData) {
            $('body').html(sinData);
        }
    });
});
            });
        </script>
    </head>

    <body>
        <div class="section-9">

            <form id="addform" method="post">
                <div class="row">

                    <div class="col-sm-12">

                        <div class="table-responsive" id="addsection">

                            <table class="table table-responsive table-hover table-striped">

                                <thead>
                                <th>Number</th>
                                <th>Price</th>
                                <th class="text-center">Installments</th>
                                <th>Contact Name</th>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><input name="addnumber" type="text" class="form-control" id="addnumber" value="" placeholder="Enter CPO Number"></td>
                                        <td><input name="addprice" type="text" class="form-control" id="addprice" value="" placeholder="Enter CPO Number"></td>
                                        <td class="text-center"><a href="#" class="btn btn-danger addi">Installments</a></td>
                                        <td><input name="addcontactname" type="text" class="form-control" id="addcontactname" value="" placeholder="Enter Contact Name"></td>
                                    </tr>
                                </tbody>

                            </table>

                        </div>

                    </div>

                    <div class="col-sm-12" id="addformajax"></div>

                    <div class="col-sm-12 margin-top-15">

                        <p><button id="btn-add-form" class="btn btn-danger btn-block" type="button">SUBMIT</button></p>

                    </div>

                </div>
            </form>
    </body>

</html>