English 中文(简体)
如何点击创建动态的列表视图的项目获取数据?
原标题:how to get data by clicking on item from listview that created dynamic?
  • 时间:2012-05-24 19:56:38
  •  标签:
  • jquery

我创建了一个动态列表视图, 我想从列表视图中的行中获取数据, 我点击该行( 数据显示在列表视图上, 因此附件工作完美), 当我单击并尝试将数据输入到“ x” 时, 我就会得到“ 未找到 ”, 我不明白原因!

我的代码:

for (var i = 0; i < json_parsed.Categories.length; i++){
        var cars= json_parsed.Categories[i];


        $( #categoryList ).append($( <li   data-categoryId = cars.car_name> ).html(  <a href="#"  onclick = "temp()" > +cars.car_name+</a> ));
}


function temp () {
   var x = $(this).data( categoryId );  
   alert (x);   
};

HAHAHAHAHAU 前的

问题回答

由于您正在使用 jQuery, 您可以在下面绑定 点击 事件 。

$( #categoryList ).on( click ,  li , function () {
    var x = $(this).data( categoryId );
    alert(x);
});

Full 代码: (您在循环中固定了几件东西)

var cars;
for (var i = 0; i < json_parsed.Categories.length; i++){
   cars = json_parsed.Categories[i];
   $( #categoryList ).append( <li data-categoryId="  + cars.car_name +  "><a href="javascript:void(0)" >  +cars.car_name +  </a> );
}    

$( #categoryList ).on( click ,  li , function () {
    var x = $(this).data( categoryId );
    alert(x);
});

<强>编辑:

我加入我的代码!

你忘了提到你正在使用jQuery 1.6.4版本。

不管怎样 点击处理器会像下面一样更改,

$( #categoryList ).delegate( li ,  click , function() {
    var x = $(this).data( categoryid );
    alert(x);
});

DEMO

然而,jQuery建议()在(jQuery 1. 7+.

您需要使用现场活动, 即一个事件Handler, 它将与元素绑在一起, 这些元素不是在页面加载时创建的, 而是以后创建的 。

// delegate to #categoryList if it already in DOM in page load

$( #categoryList ).on( click ,  li , function() {
   var x = $(this).data( categoryId );  
   alert(x);
});




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...