Jquery函数,当所有tr共享同一个类时,更改特定tr的一些HTML


jquery function to change the some html of a specific tr when all tr share the same class

所以我有这个表的多个tr具有相同的类,让我们把这个类称为"same"现在表是动态的,这意味着我可以添加多个tr与相同的类。现在在tr中是一个选择标签,onchange将调用函数type_switch,现在type_switch将根据所选选项在tr上附加一些html内容,因此更改应该只发生在特定的tr上,而不是全部,现在的问题是,起初一切都是a -ok,但是当我决定转移html内容应该添加在tr内的tr上的地方时,一切都停止了工作。这里是type_switch的代码:

function type_switch(elem,act_id){
var tr = $(elem).parent('td').parent('tr');
// var tr = ".actionrow";
var appendum = "";
if(act_id!=undefined)
appendum = "&action_id="+act_id;
$(tr).children('.type-label').html("<img src='<?php echo BASE_URL;?>admin/images/ajaxloader.gif' />");
$(tr).children('.type-selection').html("<img src='<?php echo BASE_URL;?>admin/images/ajaxloader.gif' />");

$.ajax({
url: "<?php echo BASE_URL;?>somewhere.php", data: "type="+$(elem).val()+"&method=get_type_criterions"+appendum, type: "POST", dataType: "json",
success: function(d){
if(d.error==0){
$(tr).children('.type-label').html(d.label);
$(tr).children('.type-selection').html(d.cont);
$(tr).children('.type-button').html(d.btn);
}
}
});
}

可重复添加的tr的示例:

<tr class="action-row">
<td valign="top">hello&nbsp;&nbsp;&nbsp;</td>
<td valign="top"><select name="crm_action[[id]]">
<option value="add">Add to Group</option>
<option value="transfer">Transfer to Group</option>
</select>
</td>
<td valign="top">Action:&nbsp;&nbsp;&nbsp;</td>
<td valign="top">
<select name="funnel_type[[id]]" class="funnel-type" onchange="type_switch(this[,id])">
<?php
$sql_tg = "some select";
$res_tg = mysql_query($sql_tg);
while($tg = mysql_fetch_object($res_tg)){
$sql_t = "some select";
$res_t = mysql_query($sql_t);
if(mysql_numrows($res_t)>0){
echo "<optgroup label='"{$tg->label}'">";
while($t = mysql_fetch_object($res_t)){
echo "<option value='"".$t->id."'">".$t->funnel_type_label."</option>";
}
echo "</optgroup>";
}
}
?>
</select>
</td>
<td><img src="<?php echo BASE_URL?>admin/images/action_delete.png" title="Delete" onclick="delete_row(this)" style="cursor: pointer;" /></td>
  //this is the part that i changed there should be no tr holding this 3 td but since the layout looks ugly and messy i decided to transfer it below.
      <tr class="actionrow">
  <td class="type-label" valign="top">[label]</td>
      <td class="type-selection" valign="top">[selection]</td>
  <td class="type-button" valign="top" align="right" colspan="2"></td>
  </tr>
</tr>
生成的HTML代码:
  <tr class="action-row">
 <td valign="top">CRM Action:&nbsp;&nbsp;&nbsp;</td>
      <td valign="top"><select name="crm_action[]">
 <option value="add">Add to Group</option>
 <option value="transfer">Transfer to Group</option>
 </select>
 </td>
 <td valign="top">Action:&nbsp;&nbsp;&nbsp;</td>
 <td valign="top">
 <select name="funnel_type[]" class="funnel-type" onchange="type_switch(this)">
 options to determine what to display in this tr onchange
 </select>
 </td>
 <td>
 <img src="somewhere/action_delete.png" title="Delete" onclick="delete_row(this)"     style="cursor: pointer;"></td>
 </tr>
 //this part should have been inside the above tr as you can see it the php code above
 <tr class="actionrow odd">
 <td class="type-label" valign="top"></td>
 <td class="type-selection" valign="top"></td>
 <td class="type-button" valign="top" align="right" colspan="2"></td>
 </tr>
 //end              
 <tr class="action-row">
 <td valign="top">CRM Action:&nbsp;&nbsp;&nbsp;</td>
 <td valign="top">
 <select name="crm_action[]"><option value="add">Add to Group</option>
 <option value="transfer">Transfer to Group</option>
 </select>
 </td>
 <td valign="top">Action:&nbsp;&nbsp;&nbsp;</td>
 <td valign="top">
 <select name="funnel_type[]" class="funnel-type" onchange="type_switch(this)">
 another options
 </select>
 </td>
 <td>
 <img src="somewhere/action_delete.png" title="Delete" onclick="delete_row(this)"      style="cursor: pointer;">
 </td>
      </tr>
 <tr class="actionrow odd">
 <td class="type-label" valign="top"></td>
 <td class="type-selection" valign="top"></td>
 <td class="type-button" valign="top" align="right" colspan="2"></td>
 </tr>

下面是您可能需要的一个快速示例:

$('.select').change(function(){
    $(this).parent().append(*your html here*)
});

这将把信息附加到tr中,而不是选择框中。

我相信这就是你的问题,如果不是,我将根据你的评论改变我的答案。

编辑OP请求:为了能够更改tr中的特定元素,您可以使用带有.children()、.find()和.close()的特定类。下面是一个例子:

$('.funnel-type').change(function(){
    var thisTr = $(this).parent().parent();
    thisTr.children('.type-label').html("something here");
    thisTr.children('.type-selection').html("something here");
    thisTr.children('.type-button').html("something here");
})
基本上,我已经创建了一个变量,以便更容易使用选择框的父TR (thisTr)。然后,如果每一个都是主tr的子元素,我们只需通过它自己的类指定每一个,然后对它们做任何我们想做的事情。