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/