调用模糊事件

时间:2018-03-26 06:59:31

标签: javascript jquery html ajax

假设我有两行这个表:

<tr>
    <td contenteditable="true" data-group1="<?php echo $_GET['group']; ?>" class="no1" id="nom1" name="nom1"><?php echo $item[$i]; ?></td>
</tr>

<tr>
    <td contenteditable="true" data-group2="<?php echo $_GET['group']; ?>" class="no2" id="nom2" name="nom2"><?php echo $item[$i]; ?></td>
</tr>

该表是contenteditable,这意味着,我可以更改表中的值。目前,一旦数据被操纵,一旦调用模糊事件,将捕获数据。这是我的剧本:

$(document).on('blur', '.no1', function(){
    var group= $(this).data("group1");
    var no = $(this).text();
});

$(document).on('blur', '.no2', function(){
    var group= $(this).data("group2");
    var no = $(this).text();
});

基于这个算法,假设我操纵了第一行中引用class = "no1"的数据,然后它将运行如下所示的脚本:

$(document).on('blur', '.no1', function(){
    var group= $(this).data("group1");
    var no = $(this).text();
});

但是,此表的行数不固定。因此,这个脚本没有帮助,因为我们自己根据行数定义了函数。有没有办法在调用blur事件后如何捕获特定的行信息,这样我们就不必定义相当多的类似函数。

1 个答案:

答案 0 :(得分:1)

您可以在trs上使用公共课,并在其blur上附加contenteditable td事件处理程序。

关于您的data-groupX属性,只需将其全部重命名为data-group

  

PHP

<tr class="common"> //Add your class here
<td contenteditable="true" data-group="<?php echo $_GET['group']; ?>" class="no1" id="nom1" name="nom1"><?php echo $item[$i]; ?></td>
</tr>
  

的Javascript

$(document).on('blur', '.common td[contenteditable]', function(){
  var group= $(this).data("group");
  var no = $(this).text();
});