【问题标题】:Send html data with json including onclick function [duplicate]使用包含onclick功能的json发送html数据[重复]
【发布时间】:2017-04-22 12:37:23
【问题描述】:

我正在使用datatable 并想使用 ajax.reload() 刷新表。 为此,我需要使用 json 语句发送所有表。 我的代码是这样的:

function relaod_table(){
        $data = $this->admin_model->get_user();     
        for ($i=0; $i < count($data) ; $i++) {
            $more = '<button type="button" id="'.$data[$i]['user_id'].'">test</button>';
            $data[$i]['more'] = $more;
        }
        echo json_encode(array('data' => $data));
}

接收到的数据是正确的,每个按钮都有一个id,即user_id。 问题是当我调用 id 时,它不会触发,也不会发生任何事情。

$( "#234" ).click(function() {
  alert( "Handler for .click() called." );
});

我的问题是

1:如何通过 id 触发按钮?

2:如果我想对按钮使用 onclick() 函数,当我发送 json 时,我收到错误消息,显示调用未定义函数 delete_user()。在这种情况下我可以使用 onclick 吗?

$more =  '<button type="button" id="'.$data[$i]['user_id'].'" onclick="'.delete_user().'">test</button>';

3:如何触发我点击的按钮?

提前致谢。

【问题讨论】:

  • 为什么使用user_id 作为id 属性? id 不能以数字开头。
  • 我将其更改为 bt.id,例如“bt234”,但没有任何更改。

标签: javascript html json codeigniter datatable


【解决方案1】:

试试这个

$('table').on('click', '#234', function() { 
        code here 
});

但是用类调用它并使用 data-* 参数来获取 user_id。 希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 2015-08-18
    • 2012-01-16
    • 2017-06-11
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    相关资源
    最近更新 更多