【发布时间】:2011-09-10 03:40:26
【问题描述】:
编辑:似乎我没有在我的 textarea 上使用“id”,所以问题已经解决,但我仍然遇到一些 text() 问题
var e = $('textarea#cost-'+i).val(); // e = 1 (for first iteration)
alert($('p#subtotal-'+i).text()); // alert 'subtotal' which is the text in my p#subtotal-1
$('p#subtotal-'+i).text().replaceWith(e); // <- this is the problem, i think
alert($('p#subtotal-'+i).text()); // no alert box at all
这次我确保他们都有 ID。
以下代码生成一个警告框,其中没有任何内容。 如果我将其更改为 val() 它会显示“未定义”
$('textarea.costbox').live('blur',function() {
for(var i in items) {
alert('iteration '+i);
alert('textarea#cost-'+i);
var e = $('textarea#cost-'+i).text();
alert('cost '+e);
}
});
<textarea name="cost-1" class="costbox"></textarea>
此代码的想法是在值更改时更新小计。 这是最后一块拼图。
当我在 chrome 中检查时,所有的 HTML 看起来都很正常。我的所有其他代码都运行良好。
这应该使用modernizer1.7 和jQuery1.5.1 运行,基于最近的HTML5Boilerplate。
这是相同基本事物的另一个示例 - http://jsbin.com/obeho5/3/edit
这可能很简单,但我已经被困在这里好几个小时了,看不到它。
[如果它不是很明显和简单的,我可以发布更多代码]
【问题讨论】:
-
textarea 有 id="cost-1" 而不是名称对吗?或者至少两者都有,否则我看不到该代码与 text 或 val 一起使用
-
物品应该是什么,你从哪里得到它?您是否打印出(firebug 的 console.log 或警报)“i”是什么?
-
'items' 将是一个帮助计算小计的数组,但我找到了更好的方法。而“我”不是问题,循环工作正常。