所以我有这个表有多个具有相同类的 tr,让我们称这个类“相同”现在表是动态的,这意味着我可以添加多个具有相同类的 tr。现在 tr 内部是一个 select 标记,onchange 将调用函数 type_switch,现在 type_switch 将根据所选选项在该 tr 上附加一些 html 内容,因此更改应该只发生在该特定 tr 而不是全部,现在问题是起初一切都很好,但是当我决定转移应该在 tr 内的 tr 上添加 html 内容的地方时,一切都停止了工作。有关更多信息,这里是 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 </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: </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: </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: </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: </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: </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>