如何为动态生成的按钮创建click事件?

时间:2018-11-29 12:29:24

标签: javascript jquery

我想为动态生成的按钮创建click事件。我该怎么办?

我认为该代码将起作用,但无效。如何添加功能?

  $(document).ready(function() {
    $("#keyword_1").click(function() {
      var keyword_js = document.getElementById("keyword_1").value;
      $.ajax({
          url: "list",
          type: "POST",
          async: false,
          data: {
            keyword: keyword_js.toString()
          },
          dataType: "json",
          success: function(data) {
            //button
          }
        }
      },
      error: function(request, status, error) {
        alert("code:" + request.status + "\n" + "message:" + request.responseText + "\n" + "error:" + error);
      },
      complete: function() {
        // call back method
      }
    });
  });
});

function insRow() {
  var keyword_js = document.getElementById("keyword_en").value;
  oTbl = document.getElementById("addTable");
  var oRow = oTbl.insertRow();
  oRow.onmouseover = function() {
    oTbl.clickedRowIndex = this.rowIndex
  }; //clickedRowIndex
  var oCell = oRow.insertCell();

  //Form Tag
  var frmTag = "<input type=button value=" + keyword_js + " id='keyword_1'>";
  frmTag += "<input type=button value=‘del’ onClick='removeRow()' style='cursor:hand;'>";
  oCell.innerHTML = frmTag;
}

1 个答案:

答案 0 :(得分:0)

也许您可以这样做。

file1