0

私はjqueryを初めて使用します。私の要件は、jquery関数でrowid(テーブルの各レコードの一意のID)を渡すことです。実行時にのみROWIDを取得できます。では、idがこのROWIDであるタグにクリックイベントをバインドするにはどうすればよいですか。

<a href="javascript:void(0)" id="`string(rowid(Gatepass))`">Upd</a>
<a href="javascript:void(0)" id="`string(rowid(Gatepass))`">Del</a>

$(what to pass here).bind('click',function(ev) {
    _operation(para1,para2); // function which is going to perfom action
    ev.preventDefault();
    return false;
});
4

4 に答える 4

0

ID 間に類似点がある場合は、次のような属性セレクターのいずれかを使用できます。

IDが含まれています

$('[id*="something"]')

で始まる ID

$('[id^="something"]')

http://api.jquery.com/category/selectors/

より良いアプローチは、動的に名前が付けられたすべてのアンカーをコンテナーに配置し、それを選択することです。

<div id="container">
    <a ...></a>
    <a ...></a>
</div>

次に、すべての子アンカーを選択します。

$('#container > a').click(...);
于 2012-05-21T13:05:09.370 に答える
0

非常に少ない HTML コードから適切なセレクターを見つけるのは困難です。可能であれば、マークアップでクラスを使用します。

<a class="roww" href="javascript:void(0)" id="`string(rowid(Gatepass))`">Upd</a>
<a class="roww" href="javascript:void(0)" id="`string(rowid(Gatepass))`">Del</a>

次に、 $('.roww') を使用してノードをクエリできます。

イベント ハンドラから ID を取得するには、次のようにします。

function( ev ) {
   //wrap the element with jQ:
   var jel = $(this);
   //Then access attributes with .attr() getter:
   var id = jel.attr('id');

   ... //do whatever you want now.
   ... // there's a quicker alternative to get the id without jQuery, simply: 
   ... // var id = this.id
}
于 2012-05-21T13:06:53.137 に答える
0

id を取得し、id に従って、必要なことを行います

  $('a').on('click',function(){

    var id = $(this).attr('id');

   if(id == //what you want)
   {
      //function 1
   } 
   else
   {
      //function 2
   }     

    return false;

    });
于 2012-05-21T13:24:34.123 に答える
0

ID がサーバーから動的に取得される場合は、同じ ID + ID セレクターのハッシュを関数内に配置します。

$(what to pass here) => $("'#" + string(rowid(Gatepass)) + "'")
于 2012-05-21T13:04:54.353 に答える