jquery .find()方法的奇怪问题
问题描述:
我对.find()方法有一个奇怪的问题。jquery .find()方法的奇怪问题
我有以下的html:
<table id="roleTab" style="padding: 5px; position: relative; top: 10px">
<thead>
....
</thead>
<tbody>
<TMPL_LOOP DATA_ROLES>
<tr id="<TMPL_VAR ID>">
<td><select id="role2" name="role2_<TMPL_VAR ID>" title="The role of the employee"><TMPL_VAR ROLE></select></td>
<td><input id="steps" type="text" name="steps_<TMPL_VAR ID>" size="5" title="Total amount of steps per month" value="<TMPL_VAR STEP>"></td>
<td><input id="measurable_steps" type="text" name="measurable_steps_<TMPL_VAR ID>" title="Measurable steps" value="<TMPL_VAR MEASURABLE_STEP>"></td>
<td><input id="steps_ratio" type="text" name="steps_ratio_<TMPL_VAR ID>" title="'Measurable steps'/'Steps'" value="<TMPL_VAR STEPS_RATIO>%" readonly></td>
<td style="text-align: center"><select id="reopen_rate" name="reopen_rate_<TMPL_VAR ID>" title="How many reopenes after closing the SI"><TMPL_VAR REOPEN></select></td>
<td><select id="w4p" name="w4p_<TMPL_VAR ID>"><TMPL_VAR W4P></select></td>
<td><select id="team" name="team2_<TMPL_VAR ID>"><TMPL_VAR TEAM></select></td>
<td><input id="checkbox" type=checkbox></td>
<td><input id="status2" type="hidden" name="status2_<TMPL_VAR ID>" value="<TMPL_VAR STATUS>"></td>
</tr>
</TMPL_LOOP>
</tbody>
</table>
我有以下脚本:
$(function(){
$('#steps').live("focusout", function() {
var steps = $(this).parents().parents().find('#steps').attr("value");
var meas_steps = $(this).parents().parents().find('#measurable_steps').attr("value");
var value = (meas_steps/steps)*100;
value = parseInt(value);
if(steps && meas_steps){
var t = $(this).parents().parents().attr("id");////////////////////
alert(t);////////////////////////////////////////////////////////////
$(this).parents().parents().find('#steps_ratio').attr("value", value+"%");
}
});
});
我在网页上有两行(以下循环)。 首先有id'2',第二个有id'3'。
问题是,当我更改第二行(也可以是第一行)的步骤字段,然后聚焦,则警报显示'3',但'steps_ratio'字段中的值已更改为两行。
为什么它不会仅仅改变id为'3'的'steps_ratio'?
感谢
答
$(this).parents().parents()
应该
$(this).parent().parent()
.parents()
返回所有父元素一路上涨树,.parent()
只返回直接父。
此外,您不应在页面中具有相同ID的多个元素。
非常感谢!有用! – Mike