简体   繁体   English

如何使用多个按钮拨打 AJAX 电话?

[英]How can I use multiple buttons for AJAX Calls?

<div class="form-group">
          <div >
              <a href="/addcourse">
            <button class="btn btn-primary ">add course</button>
        </a>
    <div class="center">
      
      <h1>manage courses</h1>
    </div>
    <div style="padding-left: 500px">
    <form class="form-horizontal" style="width: 50%;">
    
     
      <div class="form-group">
          <label for="faculty" class="col-sm-2 control-label" ></label>
          <div class="col-sm-10">
            <div class="btn-group">
              <button index=0 id='facultyDropdown' class="btn">Faculties</button>
              <button class="btn dropdown-toggle" data-toggle="dropdown">
                <span class="caret"></span>
              </button>
              <ul class="dropdown-menu">
                  {{#facultyId}}
                     <li><a index="{{id}}">{{faculty}}</a></li>
                  {{/facultyId}}              
              </ul>
            </div>
          </div>
      </div>
      <div class="form-group">
          <div class="col-sm-10 col-sm-offset-2">
              <input id="submit" name="submit" type="bottom" value="submit" class="btn btn-primary submit">

               <table class="table">
        <thead>
          <tr>
            <th scope="col1" >corse code</th>
            <th scope="col1" >course name</th>
            <th scope="col2">credit hourse</th>
           
            <th scope="col5"></th>
            <th scope="col6"></th>
          </tr >
          <tbody id="myTable"></tbody>
        </thead>
      </table>
          </div>
      </div>
  </form>
    </div>

This HTML page has 2 main buttons and I generated two more buttons in every table row.这个 HTML 页面有 2 个主要按钮,我在每个表格行中生成了另外两个按钮。 The two buttons in the row don't work with ajax.行中的两个按钮不适用于 ajax。

$(document).ready(function(){

  function buildTable(data){
        document.getElementById('myTable').innerHTML = "";
    
    var table = document.getElementById('myTable');
        for (var i = 0; i < data.length; i++){
            var row = `<tr>
                            <td>${data[i].code}</td>
                            <td>${data[i].course}</td>
                            <td>${data[i].id}</td>
              <td><button id=${data[i].id} name="update" type="button" value="${data[i].id}"  class="update" >update</button></td>
              <td><button id=${data[i].id} name="update" type="button" value="${data[i].id}"  class="delete" >delete</button></td>
                      </tr>`
          
            table.innerHTML += row


        }
    }
  $(".dropdown-menu li a").click(function(){
    const index = $(this).attr('index');
    $("#facultyDropdown").attr('index', index);
    $("#facultyDropdown").text($(this).text());
  });
 
  $("#submit").click(function() {

    console.log('alaa');
    
    const facultyId = $("#facultyDropdown").attr('index');

   
    $.ajax({
      type: "post",
      url: `/api/v1/faculties/`+`${facultyId}`,
      
      success:function(res){
        courses= res
      
        buildTable(courses)
        console.log('courses')
        
      }    
    });
  });  

 $(".delete").click(function() {
 
    const corseid = $(this).attr("id");
   
   console.log(corseid);

   
    $.ajax({
      type: "delete",
      url: `/api/v1/courses/`+`${corseid}`,
      
      success:function(res){
       alert(`course deleted ${res}`)
        
      }    
    });

  
  
  });  


  $(".update").click(function() {
 
    const corseid = $(this).attr("id");
   
   console.log(corseid);

   
    $.ajax({
      type: "put",
      url: `/api/v1/courses/`+`${corseid}`,
      
      success:function(res){
       alert(`course deleted ${res}`)
        
      }    
    });

  
  
  });  
      
});


 
 

This is the ajax code above, I don't understand that the droplist and the add course button and submit button is working only the update and delete buttons are not working.这是上面的 ajax 代码,我不明白下拉列表和添加课程按钮和提交按钮是否有效,只有更新和删除按钮无效。

The problem is when you create your event handler, the elements don't exist (since they only exist after clicking submit ).问题是当您创建事件处理程序时,元素不存在(因为它们仅在单击submit后才存在)。

You need to use event delegation, there are a few ways of doing that, this is probably the easiest:您需要使用事件委托,有几种方法可以做到这一点,这可能是最简单的:

$("#myTable").on("click", ".delete", function (event) {
// your delete code
});

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

相关问题 如何显示多个同步jQuery Ajax调用的进度? - How can I show progress of multiple synchronous jQuery Ajax calls? 如何在一个路由中进行多个ajax调用? - How can I have multiple ajax calls in one route? 如何使用Ajax请求来触发Javascript中的Web服务调用 - How can I use an ajax request to fire Webservice calls in Javascript 如何为多个单选按钮使用一个事件处理程序? - How can I use one event handler for multiple radio buttons? 如何在Twitter的Popover中使用“div”和多个按钮? - How can I use “div” in Twitter's Popover with multiple buttons? 如何在递归AJAX调用中提供“解析”功能,以便可以使用“完成”? - How can I serve a “resolve” in my recursive AJAX calls so that I can use 'done'? 如何在多个文件上使用 Ajax 请求? - How can i use Ajax request on multiple files? 如何修改ajax调用或promise链,以便可以从单独的ajax调用中使用多个变量(来自数组)? - How to modify ajax calls or promises chain so that multiple variables (from an array) can be used in separate ajax calls? 如何在Razor MVC 5.2.3 Ajax中使用多个提交按钮 - How to use multiple submit buttons with Razor MVC 5.2.3 Ajax 如何等待多个异步调用? - How can i wait for multiple async calls?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM