jquery数据表行单击“不工作”

jquery数据表行单击“不工作”,jquery,Jquery,我创建了一个简单的jquery数据表示例,但它的行单击事件不起作用 它显示数据,但我想在单击行时,它在警报中显示行的数据 请帮帮我 我的代码是: <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui" xmlns:h="http://java.sun.c

我创建了一个简单的jquery数据表示例,但它的行单击事件不起作用

它显示数据,但我想在单击行时,它在警报中显示行的数据

请帮帮我

我的代码是:

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:composite="http://java.sun.com/jsf/composite"
    >

<composite:interface>

</composite:interface>

<composite:implementation>

    <style type="text/css" title="currentStyle">
        @import "/resources/css/demo_table.css";
    </style>

    <script type="text/javascript" charset="utf-8">

        $(document).ready(function () {

            $('#example').dataTable(
                    {
                        "aLengthMenu": [
                            [2, 5, 10, -1],
                            [2, 5, 10, "All"]
                        ],
                        "processing": true,
                        "ajax": {
                            "url": "/DataTableServlet",
                            "dataSrc": "demo",
                            "type": "GET"

                        }
                    });

            $("#example tbody").click(function(event) {

                $(oTable.fnSettings().aoData).each(function() {
                    $(this.nTr).removeClass('row_selected');
                });
                $(event.target.parentNode).addClass('row_selected');

            });

            $("#example tbody tr").live('click', function(event) {
                var aPos = oTable.fnGetPosition(this);
                var aData = oTable.fnGetData(aPos);
                gIDNumber = aData[0];
               alert(gIDNumber);

            });


            oTable = $('#example').dataTable();
        });


    </script>


    <p:panel header="hello">
        <div id="dynamic">
            <table style="cellpadding:0 ;cellspacing:0 " border="0" class="display"
                   id="example">
                <thead>
                <tr id="zz">
                    <th style="width: 3%">First Name</th>
                    <th style="width: 3%">Last Name</th>
                    <th style="width: 3%">Address 1</th>
                    <th style="width: 3%">Address 2</th>

                </tr>
                </thead>
            </table>
        </div>
        <br/>
        <br/>
        <h:inputText id="asd" value="hello"/>
    </p:panel>


</composite:implementation>


</ui:composition>

@导入“/resources/css/demo_table.css”;
$(文档).ready(函数(){
$('#示例')。数据表(
{
“阿伦哲努”:[
[2, 5, 10, -1],
[2,5,10,“全部”]
],
“处理”:对,
“ajax”:{
“url”:“/DataTableServlet”,
“dataSrc”:“demo”,
“类型”:“获取”
}
});
$(“#示例tbody”)。单击(函数(事件){
$(oTable.fnSettings().aoData)。每个(函数(){
$(this.nTr).removeClass('row_selected');
});
$(event.target.parentNode).addClass('row_selected');
});
$(“#示例tbody tr”).live('单击',函数(事件){
var aPos=可旋转的.fGetPosition(此);
var aData=oTable.fngedata(aPos);
gIDNumber=aData[0];
警报(gIDNumber);
});
oTable=$(“#示例”).dataTable();
});
名字
姓
地址1
地址2


我想我必须在$(文档)中插入click函数。是吗?

试试这个

$(document).on('click', '#example tbody tr', function(event){
                var aPos = oTable.fnGetPosition(this);
                var aData = oTable.fnGetData(aPos);
                gIDNumber = aData[0];
               alert(gIDNumber);

            });
我想我必须在$(文档)中插入click函数。是吗?

不,这样使用是正确的,但您尚未发布jQuery版本。如果您有
version 1.9+
,则必须使用
。on()
jQuery方法委派事件:

$("#example").on('click', 'tbody tr', function(event) {
   var aPos = oTable.fnGetPosition(this);
   var aData = oTable.fnGetData(aPos);
   gIDNumber = aData[0];
   alert(gIDNumber);
});
使用
.on()
是因为在jQuery的
1.9+
版本中删除了
.live()


你的数据库在哪里?你使用的是什么版本的jQuery。谢谢我的朋友。成功了。谢谢你的帮助。希望好运。我的朋友,我还有其他问题。我希望你能帮助我。我想在其中一个单元格中插入一个按钮,该按钮的标签就是该单元格的值。我该怎么做?我的朋友,它成功了。我还有其他问题,希望你能帮助我。我想在其中一个单元格中插入一个按钮,该按钮的标签就是该单元格的值。我该怎么做?我的代码在代码上方,我将其更改为您所说的