简体   繁体   English

如何做POST和GET请求同一个按钮jQuery

[英]How to do POST and GET request same button jQuery

I have been stuck on this same problem now for 2 weeks with 10-15 hours a day. 我一直在这个相同的问题上停留了2周,每天10-15个小时。

I am using dynamic buttons that append more buttons which work like tabs on my view which are dynamic. 我正在使用动态按钮,这些按钮附加了更多按钮,这些按钮的工作方式类似于视图中的动态标签。

I can only use 1 form for this so the dynamic buttons are being used to know which model ID to load. 为此,我只能使用1种形式,因此使用动态按钮来了解要加载的型号ID。

I have to use only 1 form Simple jquery is this: 我只需要使用1种形式的简单jquery是这样的:

$("#copy-link").click(function (e) {
    e.preventDefault();

    var num_tabs = $("div#tabSequence ul li").length + 1;
    $("div#tabSequence ul").append(
        "<li class='tab-button'><a data-toggle='tab' id='link" + num_tabs + "' href='#tab" + num_tabs + "'>#" + num_tabs + "</a></li>"
    );

    var data =  $('#campaign-form').serialize();



      $.ajax(
            {
                dataType: 'html',
                type: 'POST',
                url: "campaigns/sequencesave",
                data: data,
                success: function(response){
                    var campaignId = response;
                    var test2 = document.getElementById("link" + num_tabs);
                    console.log(test2);
                    test2.setAttribute("data-campaign", campaignId);
                }

            }
    )
});

Is there a way I can POST form data and also load form content for the Tab button clicked? 有没有办法我可以发布表单数据并为单击的“ Tab”按钮加载表单内容?

$("body").on("click", ".nav-tabs li a", function() {
        var data =  $('#campaign-form').serialize();


        $.ajax(
            {
                dataType: 'html',
                type: 'POST',
                url: "campaigns/sequenceupdate",
                data: { 'campaign_uid' : $(this).attr('data-campaign'), csrf_token: csrfTokenValue },

            }
        )
    })

^^ that same button clicked has to save what is currently on the view and then load contents for the button clicked. ^^所单击的同一按钮必须保存视图中当前的内容,然后加载所单击按钮的内容。 I am appending an ID of the Model needed to the button when user clicks #copy-link and saving it automatically 当用户单击#copy-link时,我会将模型的ID附加到按钮上并自动保存

Im not a jQuery wiz but you can do this with straight up javascript in conjunction with Jquery. 我不是jQuery wiz,但是您可以结合Jquery使用纯正的javascript来做到这一点。

function postInfo(event){
    httpRequest = new XMLHttpRequest();
    if (!httpRequest) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
    }
    httpRequest.onreadystatechange = results;
    httpRequest.open('POST', 'ENTER YOUR URL HERE');
    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httpRequest.send('serverpin='+ServerPIN+'&restuarantid='+RestaurantID); //<-these are the variables you are posting
  }

function results(){
    if (httpRequest.readyState === XMLHttpRequest.DONE) {
      if (httpRequest.status === 200) {
        // WHATEVER YOU WANT TO MAKE IT DO IF THE REQUEST IS SUCCESSFUL
      } else {
        alert('There was a problem with the request.');
      }
    }
  }

And then to invoke it I use an event listener like this 然后调用它,我使用这样的事件监听器

document.getElementById('WHATEVERIDMATTERS').addEventListener('click',postOrder);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM