dtx9763
2013-01-11 12:58
采纳率: 100%
浏览 1.5k
已采纳

从html onclick事件传递jQuery onclick函数的参数

我希望从html onClick事件中将参数传递给onClick jQuery函数。

PHP中的HTML语句呈现:

$name = "abcd";
for ($i=0;$<10;$i++){
$text .= '<tr>
             <td>'.$i.'</td>
             <td><div id="name">'.$name.'/div>
                 <a class="href" onclick="callJSFunc($i,\''.$name.'\')" ></a>
             </td>
          </tr>';
}

javascript:

function callJSFunc(i , name){
alert(i+"  "+name);
}

我想把它变成这样子:

$(".href").click(function(i,name,event){
alert(i+"  "+name);
});

图片转代码服务由CSDN问答提供 功能建议

我想从html onclick事件中将参数传递给onclick jquery函数。

PHP中的html语句呈现

  $ name =“abcd”; 
for($ i = 0; $&lt; 10; $ i ++)  {
 $ text。='&lt; tr&gt; 
&lt; td&gt;'。$ i。'&lt; / td&gt; 
&lt; td&gt;&lt; div id =“name”&gt;'。$ name。  '/ div&gt; 
&lt; a class =“href”onclick =“callJSFunc($ i,\''。$ name。'\')”&gt;&lt; / a&gt; 
&lt; / td&gt; 
  &LT; / TR&GT;'; \ N} 
   
 
 

<强>的JavaScript ..

 <代码 > function callJSFunc(i,name){
alert(i +“”+ name); 
} 
   
 
 

我想把它转换成某些东西。 ..

  $(“。href”)。click(function(i,name,event){
alert(i +“”+ name); \  n}); 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanmei1930 2013-01-11 13:03
    已采纳

    Why don't you try in some other way

    $name = "abcd";
    for ($i=0;$<10;$i++){
    $text .= '<tr>
             <td>'.$i.'</td>
             <td><div id="name">'.$name.'/div>
                 <a class="href" data-id="'.$i.'" data-name="'.$name.'" ></a>
             </td>
          </tr>';
    }
    

    And in jQuery onClick

    $(".href").click(function(){
        alert($(this).attr('data-id')+"  "+$(this).attr('data-name'));
    });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drtiwd06558 2013-01-11 13:01

    You cannot change the way events are fired but you can always store those parameters as data-* attributes in markup and extract them inside the event handler. What you request is to subscribe for the event and the event object to naturally mutate according to what you desire as parameters, however the browser has no way to figure out what you require.

    What I mean by using data-* attributes is to store the data inside your DOM element:

    <a class="href" data-index="$i" data-name="$name" href="someHref" />
    

    jQuery:

    $('.href').click(function (event) {
        var index = $(this).attr('data-index'); // Yields a string use data() for other data types
        var name = $(this).attr('data-name'); // Yields a string use data() for other data types
    });
    
    评论
    解决 无用
    打赏 举报
  • dongwu3747 2013-01-11 13:14

    It is better to do this with jQuery instead of sending the parameters with PHP.

    You can do something like this in your javascript:

    $('a.href').click(function( event ) {
      var index = $('table').index(this); // Containing table
      var name = $(this).siblings('#name').innerHTML;
      alert( index + ' ' + name );
    });
    

    Btw, an id should be unique. Your code generates 10 table rows with in every one of them the id '#name'.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题