0

こんにちはすべて私は以下のように動的にテーブルを作成するときにIDとクラスを宣言しています私はクリックイベントを実行するために多くの関数を使用していますが、すべての静脈で、plsは誰かができるなら私を助けますか?

これは、IDとクラスを持つ動的テーブルです。「td」クリックでcssを呼び出す必要があり、クリックイベントも発生する必要があります。

   var tableid=0;               
    // display table.......
        $('#page').append("<center> <table id='tables'  cellspacing='50px' style='margin-top:70px' >");
        for(var j=0;j<row;j++){          
                $('#tables').append("<tr class='row' id='row"+j+"' style='margin-bottom:50px'>");
                        for(var k=0;k<5;k++){
                                if(tableid<tablecount-1){
                                        $("#row"+j).append("<td id='"+tableid+"' class='button' width='150' height='150' background='images/green.png' align='center'>"+
                                                            "</td>");   
                                        tableid++;
                                    }
                        }//for ends.                    
                        $('#tables').append("</tr'>");
        }//for ends
        $('#page').append("</center> </table>");

すべてのtdに対して個別にイベントを発生させたいので、クリックすると、自分のIDをパラメーターとして渡す関数を呼び出します。私は「td」ごとに一意のIDを使用しています。以下にコメントするコードは、クリックイベントを実装/取得し、cssを実行するように呼び出しようとしたメソッドですが、すべてが順調です。

/*Event.observe(window, 'load', function(){
            $('#TBL00001').observe('click', test());
            });             
            function test(){
                alert("click :)))");
            }
            $("'#"+tableCode[tableid]+"'").live(function() {
                    alert("Table"+tableCode[tableid]);
            });
            $("'#"+tableCode[tableid]+"'").click(function() {
                        alert("Table"+tableCode[tableid]);
            });

            $('#TBL00001').on( 'click', 'td', function() {
                        alert("Table"+tableCode[tableid]);});
            */

今、私は.delegate()も試しましたが、これは機能しますが、各tdのID(一意)を渡すことはできません

                $('.button').each(function(){
                $(this).click(function{
                    alert("hi");
                });
           });


              $("tr .row > td .button").each(function() { 
            var $this = $(this);
            alert("hi"+$this);
        }

         /*$("table").delegate($(this),"click",function(){
            alert($(this));
            printBill(tabid);
        });
4

5 に答える 5

1
$('table').delegate('tr.row > td', 'click', function() {
    id = $(this).attr('id');
}

あなたの質問を理解できるかどうかわかりません。

このコードはtdをクリックするたびに実行され、変数idにはtdのidが含まれます。

于 2012-06-21T07:09:45.030 に答える
0

イベントの委任で使用する必要があります

$("table").on("click",function(e){
alert(jquery(e.target).attr('id');
})
于 2012-06-21T07:11:17.320 に答える
0

ファイアバグでtrのIDを見たことがありますか?

このように割り当てると、間違っている可能性があります。

  id='"+tableid+"'

これを試して。

id="+tableid+"

幸運を..

于 2012-06-21T07:17:13.667 に答える
0

あなたのコードには条件がありますが、どこにif(tableid<tablecount-1){も反抗していないと思うtablecountので、条件は機能していません。

次に、上記のように、tdの場合、次の方法で各IDをフェッチできます:-

$('td').click(function(){
   var tdId = $(this).attr('id');
   console.log(tdId); // You would get the Id of clicked td in teh consloe and now you can  use the variable **tdId** for your function.
});
于 2012-06-21T07:17:36.427 に答える
0

テーブルを作成するというあなたのアプローチにあります。このようにテーブルを作成する必要があります。

 $(function(){
var tableid=0;
var row = 5;

// display table.......
var theTable = $('<table />').attr('id', tableid);

for(var j = 0; j < row; j++){
    var theRow = $('<tr />').attr('id', 'row'+j).addClass('row');
    for(var k=0;k<5;k++){
        var theTd = $('<td />').addClass('button').html('test').click(function(){ alert('test') });
        theRow.append(theTd);
    }
    theTable.append(theRow);
}

$('#page').append(theTable);
    });
于 2012-06-21T08:56:40.313 に答える