1

I have this in my view

<script type='text/javascript'>
$(function(){       
    $('#users').click(function(){
        alert($("tr[aria-selected=true] td:first").html())                      
    })
})
</script>

And inside my users.js

$(function(){       
    $('#users').click(function(){
        alert($("tr[aria-selected=true] td:first").html())                      
    })
})

Having them,obviously not at the same time,gives me different results Basically I'm trying to retrieve the ID of the selected row finding it attr value.\

With the unobtrusive way right after loading the page I'd get "null" on my first click on a row and then every time I hit a row shows me the value that I clicked previously...TWICE!. But when keeping the script inside my view I get the result I want.

What am I doing wrong?

EDIT:

My view

<%=raw jqgrid("List of users", "users", "/users", 
 [    
   { :field => "id", :label => "id",:width => 50 },    
   { :field => "username", :label => "username",:width => 120,:editable => true },    
   { :field => "email", :label => "email",:width => 120,:editable => true },    
   { :field => "name", :label => "name",:width => 100,:editable => true },    
   { :field => "lastname", :label => "lastname",:width => 100,:editable => true },
   { :field => "groups", :label => "roles",:width => 180}   
 ] , {:add => true,:edit => true,:delete => true,:error_handler => "s",:inline_edit => 'true',:edit_url => post_data_users_url,:sortable_rows => 'true'}  
 ) %>
 <p><%= link_to "Edit roles",roles_path%>
 <%= link_to "| Edit user",{:action => 'edit'},{:class => 'editLink hide'}%></p>

 <script type='text/javascript'>
   $(function(){
       var _last = new String()     
       $('#users').click(function(){
           _last = $("tr[aria-selected=true] td:first").html()
           $(".editLink").attr("href","/users/edit/"+ _last)
           $(".editLink").addClass('show')
       })
   })
</script>

My layout

<!DOCTYPE html>
<html>
<head>
  <title>UserManager</title>
  <%= stylesheet_link_tag "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
  <%=jqgrid_javascripts%>
  <%=jqgrid_stylesheets%>
</head>
<body>
    <div>
        <%if flash[:notice]%>
            <p><%= flash[:notice]%></p>
        <%end%>
        <%if current_user%>
            Logged in as <%= current_user.username%> | <%= link_to 'logout', log_out_path%>
        <%end%>
    </div>
    <%= yield %>
 </body>
</html>

The way I see it...nothing special there

4

1 回答 1

0

根据您的性能要求,使用 .live('click', function(){}) 而不是 click(function(){}),或者在 users.js 中创建一个函数来进行绑定并使用回调jqgrid 来运行它。

在 users.js 中

$(function(){       
    $('#users').live('click', function(){
        alert($("tr[aria-selected=true] td:first").html())                      
    })
})

或者在 users.js 中:

function bindClickToRow(){
   $(function(){       
     $('#users').click(function(){
        alert($("tr[aria-selected=true] td:first").html())                      
     })
   })
 }

然后在 jqgrid 的回调中使用它(我不知道他们的 API 是如何工作的,我无法快速找到,但我很确定一个好的网格插件支持这一点,所以将其视为伪代码):

jQuery('table').jqgrid({
  afterCreate: bindClickToRow
})

希望这可以帮助。

据我所知,原因是您正在使用“点击”将点击事件绑定到一个函数。但是当实际本身还不存在时,您正在这样做。所以你基本上是在告诉 jQuery,在现在存在的所有 s 上绑定“点击”。

如果你使用 'live' 会发生什么,这相当于告诉 jQuery 将该函数绑定到与你的选择器匹配的所有 current AND future 上的一个 'click' 事件。

尚不存在的原因是因为显然 jqgrid 是在被调用的绑定之后加载的。此外,jqgrid 可能会在调用“click”绑定很久之后定期创建新的 s。如果你使用'live',jQuery 会留意所有新的,并在它们创建后立即将回调函数绑定到它们。

于 2012-04-06T19:56:29.697 回答