Onclick事件Javascript和JQuery

Onclick事件Javascript和JQuery

我的main.js文件中包含以下代码段

function one(){
  //some code
  var btn = "<button type='submit' id='processReceipt' class='btn btn-primary' onclick='form_new_receipt()'>Submit</button>";

  document.querySelector('#showGrower').innerHTML += btn;
}


提交按钮正在调用的功能如下

$(document).ready(function() {
  // Do your event binding in JavaScript, not as inline HTML event attributes:
  $("#processReceipt").on("click", form_new_receipt);
  function form_new_receipt() {
    alert('before function');
  }
});


由于警报未显示,我可能会错过什么-这意味着未调用该函数。

我已经在下面尝试过了,但是没有用-这就是为什么我添加了$(document).ready

function form_new_receipt() {
  alert('before function');
}

最佳答案

这是您在找什么吗?



function one(){
var btn = "<button type='submit' id='processReceipt' class='btn btn-primary' onclick='form_new_receipt()'>Submit</button>";
$('#showGrower').append(btn);
}

$(document).on('click', '#processReceipt', function() {
     one()
});

function form_new_receipt(a) {
    alert('before function');
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="showGrower"></div>
<button id="processReceipt" type="button">Click me</button>

关于javascript - Onclick事件Javascript和JQuery,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47155315/

10-11 05:36