0

checking the checkbox preferablyマウス クリックまたは以下の html テーブルから行の値を取得するにはどうすればよいですか?

これはjs、Spryを使用してxmlからテーブルの値を取得するためのものです

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();

データが入力されるテーブルを含むDivwithリージョンを次に示します(js の部分を参照) 。sprypv1

<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>

以下のコードを試していますが、それでもアラートが表示されないため、どの回答も機能していません。構文 n all はすべて正しいことは知っていますが、ここで何が問題なのかわかりません!

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

私のページには、一部のコンテンツを整列させるための他のテーブルもあります。したがって、以下のコードを使用すると、問題のテーブル以外のテーブルで完全に機能します。trSpry デ​​ータセットによって入力されるテーブルには2 つしかないため、適切に識別されないため、問題になる可能性があります。たぶん、よくわかりませんが、私の理解を向上させようとしているだけです

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

3 に答える 3

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() );
       }
    };
});
于 2011-05-12T08:34:01.647 に答える
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 を使用する代わりに、jQuery live メソッドを使用します。

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

これにより、クリック イベントが現在のすべてのテーブル行と、将来追加される可能性のあるすべてのテーブル行にバインドされます。ここでjQueryのドキュメントを参照してください

于 2011-05-12T08:32:13.080 に答える
0

次のようなSpryObserverを使用する必要があります。

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);
于 2012-05-23T17:35:40.783 に答える