2010-08-24 59 views
2

只需要寻找最佳实践方法即可。 我有一个表格列表信息,最后一列是一个带有“编辑/查看”的按钮。当用户点击按钮上显示一个div面积可与JSTL将var传递给jquery的function()js

<script type="text/javascript"> 
//Click on Edit/View on table 
$('.viewCustomer').click(function() 
{ 
    ....... 
}); 

</script> 

<tr class="odd"> 
    <td>${customerBean.comName}</td> 
    <td>${customerBean.comCode}</td> 
    <td class="noRightPad"> <input type="submit" name="createBut" value="View/Edit" class="viewCustomer" /> </td> 
</tr> 

的一些片断进行编辑下面

代码的详细信息所以我的问题是:

(1)如何(函数()

(2)这是要做到这一点的最好方法吗?是否有更高效/安全/更清洁的做法这个?

干杯 Alexis

回答

2

点击功能不会被您调用。单击该按钮时,这就是所谓的,因此已经将事件对象传递给它:

$('.viewCustomer').click(function(evt){ 
    ....... 
}); 

究竟是什么你想传递什么?你可以使用this$(this)来访问你点击的DOM元素,所以也许可以从这里引用你想要的。

编辑发表评论

如果用户在按钮 是表第四行和 该行的另一科拉姆有 客户ID 1234点击我想通过 可变1234

注:的下面都没有被测试,但应该足够

我们假设你的'客户ID'栏有一个'customerid'的类名。所以你的HTML可能是:

<tr class="odd"> 
    <td>${customerBean.comName}</td> 
    <td class="customerid">${customerBean.comCode}</td> 
    <td class="noRightPad"> <input type="submit" name="createBut" value="View/Edit" class="viewCustomer" /> </td> 
</tr> 

jQuery的可能看起来像:

$('.viewCustomer').click(function(){ 
    var $buttonCell = $(this).parent(); //the <td> containing the button 
    var $buttonRow = $buttonCell.parent(); //the <tr> containing the button and your customer id 
    var $customerIdCell = $buttonRow.find("td.customerid"); 
    var customerId = $customerIdCell.text(); 
}); 

以上是proken分解成线向您展示如何的东西被取出。使用“链接”我们可以更简明地表达出来:

$('.viewCustomer').click(function(){ 
    var customerId = $(this).parent().parent().find("td.customerid").text(); 
} 

您也可以搜索在客户单元作为一个更简洁的方法(一个少函数调用)的纽扣电池的“兄弟”。

$('.viewCustomer').click(function(){ 
    var customerId = $(this).parent().siblings("td.customerid").text(); 
} 
+0

嗨, 我期待通过一个字符串变量。因此,如果用户点击了位于表格第4行的按钮,并且在该行中,另一个客户有客户ID 1234,我想要传递变量1234. 干杯 alexis – alexis 2010-08-24 14:05:59

+0

James谢谢! 那正是我需要的。 Alexis – alexis 2010-08-25 10:53:13