javascript - 在 onclick 函数中发送数组

标签 javascript jquery

function show(array){
  console.log(array);
}

function main() {
  var send = ["HOLA", "ADIOS"];
  $('#table').append('<tr>'+
                    '<td style="width: 35%">Oscar</td>'+
                    '<td style="width: 6%">'+
                        '<button type="button" class="btn btn-success" data-dismiss="modal" onclick=show('+send+')>+</button>'+
                    '</td>'+
                '</tr>'
   );
}
main();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table" class="table table-bordered">
	<thead>
		<tr>
		  <th style="width: 35%" id="wallet-modal-table-name-header"></th>
			<th style="width: 6%"  id="wallet-modal-table-options-header"></th>
		</tr>
	</thead>
</table>

我有这段代码,我想发送数组“send”,但是当我将它作为函数中的参数发送时,它显示一个错误。我能做什么?

最佳答案

对于动态创建的元素,请使用事件委托(delegate)方法:

$(selector4parent).on(event, selector4children, handler);

function show(array) {
  console.log(array);
}

function main() {
  $('#table').on('click', '[type="button"]', function(e) {
    var send = ["HOLA", "ADIOS"];
    show(send);
  });


  $('#table').append('<tr>' +
    '<td style="width: 35%">Oscar</td>' +
    '<td style="width: 6%">' +
    '<button type="button" class="btn btn-success" data-dismiss="modal">+</button>' +
    '</td>' +
    '</tr>'
  );
}
main();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table" class="table table-bordered">
  <thead>
    <tr>
      <th style="width: 35%" id="wallet-modal-table-name-header"></th>
      <th style="width: 6%" id="wallet-modal-table-options-header"></th>
    </tr>
  </thead>
</table>

关于javascript - 在 onclick 函数中发送数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50356954/

相关文章:

javascript - 拉斐尔如何移动文本

javascript - 过滤我的@html.dropdownlist

javascript - 什么是 react 中的渲染劫持?

javascript - 根据嵌套键值计算对象

javascript - 拖动项目时如何在Extjs中创建占位符

Jquery onclick 事件改变 Font Awesome 图标

javascript - jQuery 对象通过键获取值

javascript - 在中间插入字符时自定义掩码功能不起作用 Javascript/JQuery

javascript - 一次只允许一个 draggable 在一个 droppable 中,允许任何一个被删除

javascript - 在没有 jquery 的情况下使用 javascript 淡入