阻止表单自动提交ajax laravel

时间:2015-09-23 04:53:03

标签: javascript jquery ajax forms laravel

我使用address_iddatabase获取了数据,我正在使用javascript ajax对话框向用户显示该数据。但即使在用户选择confirm选项后,也会提交cancel,但我想仅在用户希望通过在确认框中选择form时继续提交表单。这是我的OK

code

和我的 <form class="form-horizontal form-bordered" method="post" id="ncell" action="formaction"> <input type="hidden" name="_token" value="{{csrf_token()}}"> <div class="form-group"> <label class="col-md-3 control-label" for="inputSuccess">Amount</label> <div class="col-md-6"> <select class="form-control mb-md" name="amount" id="amount"> <option value="10">10</option> <option value="20">20</option> <option value="30">30</option> <option value="40">40</option> <option value="50">50</option> </select> </div> </div> <div class="input-group mb-md"> <button type="submit" class="btn btn-warning btn-sm" id="sub">Submit</button> </div> </form>

javascript

当用户点击<script> $("#ncell").submit(function(e){ var selectedVal= $("select option:selected").val(); $.ajax({ type:"GET", url: "/getdollarvalue",//put y data:{ value:selectedVal}, contentType: "application/json; charset=utf-8", dataType: "Json", success: function(result){ return confirm(result.nrs); // Note Send the Json Object from the server side } }); e.preventDefault(); }); </script> 按钮时,我正在收到确认对话框。我只想在用户点击submit对话框中的OK时继续进一步操作。

4 个答案:

答案 0 :(得分:2)

mDescriptionTextView.setText(mInjury.getDescription());

或者您可以使用<script> $("#ncell").submit(function(e){ var selectedVal= $("select option:selected").val(); $.ajax({ type:"GET", url: "/getdollarvalue",//put y data:{ value:selectedVal}, contentType: "application/json; charset=utf-8", dataType: "Json", success: function(result){ if(confirm(result.nrs)){ $("#ncell").submit(); } } }); return false; }); </script> 代替e.preventDefault();

答案 1 :(得分:0)

如果您想在用户按下确定按钮时提交表单,则需要将单击事件绑定到ok按钮以提交表单。 见下面的例子。

$('.ok-btn').click(function(){
    var data = $('form').serialize();
     // your ajax to submit the form and handle the response.
    $.ajax({

    })
});
希望这有帮助。

答案 2 :(得分:0)

您可以将按钮的按钮类型从提交更改为按钮,如

<input type="button" class="btn btn-warning btn-sm" id="sub"/>

避免自动提交

答案 3 :(得分:0)

您可以在函数中调用ajax并从确认对话框函数调用它 你也应该在提交点击时添加return false以防止默认表单提交操作:

        $('#submit').on('click', function() {
              confirmFunction('Are you sure you want to...');
              return false;
            }

            function confirmFunction(message) {
              //show confirm dialog ( ok - cancel )
              $('#confirm').show();
              $('#confirm p').innerHTML(message);
              $('.ok').on('click', function(){
                ajaxSubmit();
                $('#confirm').hide();
              });
              $('.cancel').on('click', function(){
                $('#confirm').hide();
              });
            }

            function ajaxSubmit() {
              var selectedVal = $("select option:selected").val();
              $.ajax({
                type: "GET",
                url: "/getdollarvalue", //put y
                data: {
                  value: selectedVal
                },
                contentType: "application/json; charset=utf-8",
                dataType: "Json",
                success: function(result) {
                  return confirm(result.nrs);
                }

              });
            };

相关问题