weixin_33737134 2014-03-07 23:15 采纳率: 0%
浏览 15

AJAX表单-组合函数

I have a form which checks if an item of stock is available in my ecommerce project that I am working on. You can see in the form, I have 2 buttons, and in the formm and to make the first function work, I have used the line <form class="text-center" onsubmit="addclient();return false"> I have 2 other functions which add the client and move the client. This line of code adds the client, there is a second one to move the customer. But I can only call this "register" function.

How can I incorporate the 2nd function into this form using the second button. I am very new to ajax and this is pretty complicated stuff. Hope someone can share their expertise. I appreciate this is a noob question

Thanks in advance for helping me out

I have 2 functions:

function checkavailability() {
    jQuery("#loading1").slideDown();
    jQuery.post("cart.php", { a: "options", sld: jQuery("#sld").val(), extension: jQuery("#extension").val(), checktype: 'register', ajax: 1 },
    function(data){
        jQuery("#results").html(data);
        jQuery("#results").fadeIn();
        jQuery("#topsay").hide();
        jQuery("#loading1").slideUp();

    });
}

The Second Function

function moveclient() {
    jQuery("#loading1").slideDown();
    jQuery.post("cart.php", { a: "options", sld: jQuery("#sld").val(), transfer: jQuery("#transfer").val(), checktype: 'transfer', ajax: 1 },
    function(data){
        jQuery("#results").html(data);
        jQuery("#results").slideDown();
        jQuery("#loading1").slideUp();
    });
}

The Form

<form onsubmit="checkavailability();return false">
  <input type="hidden" name="direct" value="true" />

     <input name="sld" id="sld" type="text" />  

     <div class="col-xs-12 col-sm-2">    
          <select>
              <option>Date1</option>
              <option>Date2</option>
              <option>Date3</option>
              <option>Date4</option>

           </select>
      </div>

    <button type="submit" class="button">Move</button>
    <button type="submit" class="button">Register</button>

</form>
  • 写回答

1条回答 默认 最新

  • weixin_33744854 2014-03-07 23:47
    关注

    You can change the buttons type to button, add a class cart, and add data attribute data-transfer to difference the buttons action.

    <form id="form_id">
        ...
        <button type="button" class="button cart" data-transfer="transfer">Move</button>
        <button type="button" class="button cart" data-transfer="register">Register</button>
        ...
    

    And in your script:

    $(".cart").click(function(){
          var transfer_type = $(this).data("transfer");
          $("#loading1").slideDown();
          $.post("cart.php", { a: "options", sld: jQuery("#sld").val(), transfer: jQuery("#transfer").val(), checktype: transfer_type, ajax: 1 },
          function(data){
              HERE CODE RESPONSE
              $("#results").html(data);
              $("#results").slideDown();
              $("#loading1").slideUp();
              $("#form_id").submit();
          });
    });
    
    //Replace addClient() for submit event
    $("#form_id").submit(function(e){
        e.preventDefault();
        //if need AJAX
        //CODE AJAX
        // or SUBMIT (POSTBACK)
        //this.submit();
        alert('SUBMIT');
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误