如何在鼠标单击事件上从动态表中选择一行

时间:2011-05-12 08:23:18

标签: javascript jquery html spry

如何从鼠标下方获取行值或从下面给出的html表中获取checking the checkbox preferably

以下是使用spry

从xml获取表格值的js
var ds1 = new Spry.Data.XMLDataSet("xml/data.xml", "rows/row"); 
var pv1 = new Spry.Data.PagedView( ds1 ,{ pageSize: 10 , forceFullPages:true, useZeroBasedIndexes:true});
var pvInfo = pv1.getPagingInfo();

以下是Div spry区域,其中包含从pv1填充的表格(请参阅js部分)

<div id="configDiv" name="config" style="width:100%;" spry:region="pv1">

    <div spry:state="loading">Loading - Please stand by...</div>
    <div spry:state="error">Oh crap, something went wrong!</div>

    <div spry:state="ready">

    <table id="tableDg" onclick="runEffect('Highlight', 'trEven', {duration: 1000, from: '#000000', to: '#805600', restoreColor: '#805600', toggle:true}, 'Flashes a color as the background of an HTML element.')"
    style="border:#2F5882 1px solid;width:100%;"  cellspacing="1" cellpadding="1"> 

    <thead>
     <tr id="trHead" style="color :#FFFFFF;background-color: #8EA4BB"> 
         <th width="2%"><input id="chkbHead" type='checkbox' /></th>
         <th width="10%" align="center" spry:sort="name"><b>Name</b></th> 
         <th width="22%" align="center" spry:sort="email"><b>Email</b></th> 

     </tr>
     </thead>

     <tbody spry:repeat="pv1">   
     <tr class="trOdd"   
     spry:if="({ds_RowNumber} % 2) != 0" onclick="ds1.setCurrentRow('{ds_RowID}');"
        style="color :#2F5882;background-color: #FFFFFF"> 
         <td><input type="checkbox" id="chkbTest" class = "chkbCsm"></input></td>
         <td width="10%" align="center">&nbsp;&nbsp;{name}</td> 
         <td width="22%" align="center">&nbsp;&nbsp;{email}</td> 

     </tr> 

     <tr class="trEven" name="trEven" id="trEven"
     spry:if="({ds_RowNumber} % 2) == 0" onclick="ds1.setCurrentRow('{ds_RowID}');"
        style="color :#2F5882;background-color: #EDF1F5;"> 
         <td><input type="checkbox" class = "chkbCsm"></input></td>
         <td id="tdname" width="10%" align="center">&nbsp;&nbsp;{name}</td> 
         <td width="22%" align="center">&nbsp;&nbsp;{email}</td> 

     </tr>
     </tbody>
     </table> 
     </div>
     </div>

我正在尝试下面的代码,但我仍然没有收到警报,因此没有任何答案也无法正常工作。我知道语法都是正确的,但我无法弄清楚这里有什么问题!

//inside $(document).ready(function()
$("#chkbHead").click(function() {
    alert("Hi");
});

我的页面也有其他表格用于对齐某些内容。因此,当我使用下面的代码时,除了问题中的那个之外,它在那些表上完美地工作。这可能是问题,因为表中只有2 tr由spry数据集填充,因此无法正确识别。可能是,我不确定,只是想帮助提高我的理解

$('tr').click(function() {
    alert("by");
});

3 个答案:

答案 0 :(得分:2)

您将获得的行的值:

$('#tableDg tbody tr').live( 'click', function (event) {
    $(this).find('td').each( function( index, item ) {
       if ( $(this).has(':checkbox') ) {
          alert( $(this).find(':checkbox').val() );
       } else {
          alert( $(this).text() );
       }
    };
});

答案 1 :(得分:1)

表格行的价值究竟是什么意思?您可以像这样获取表格行的内部html:

var html = '';
$('tr').click(function() {
    html = $(this).html();
});

您可以像这样获取表格行的属性(例如它的Id):

var id = '';
$('tr').click(function() {
    id = $(this).attr('id');
});

或者,您可以获取嵌套元素的值,例如文本输入,如下所示:

var text = '';
$('tr').click(function() {
    text = $(this).find('#myTextBox').val();
});

修改

这是如何更改嵌套在表格行中的复选框的checked属性:

$('tr').click(function() {
    $(this).find('input:checkbox').attr('checked', 'checked');
    // alternatively make it unchecked
    $(this).find('input:checkbox').attr('checked', '');
});

修改

由于表行是动态加载的 - $()。click()事件绑定方法不起作用,因为当你调用它时 - 表行不存在,所以click事件不能绑定到它们。而不是使用$()。click使用jQuery live方法:

$('tr').live('click', function() {
    // do stuff
});

这会将click事件绑定到所有当前表行和将来可能添加的所有表行。 See the jQuery docs here

答案 2 :(得分:0)

你必须使用 Spry Observer , 像这样的东西:

function funcObserver(notificationState, notifier, data) {
    var rgn = Spry.Data.getRegion('configDiv');
    st = rgn.getState();
    if (notificationState == "onPostUpdate" && st == 'ready') {
        // HERE YOU CAN USE YOUR JQUERY CODE
        $('#tableDg tbody tr').click(function() {
            $(this).find('input:checkbox').attr('checked', 'checked');
            // alternatively make it unchecked
            $(this).find('input:checkbox').attr('checked', '');
        });
    }
}
Spry.Data.Region.addObserver("configDiv", funcObserver);