jQuery text() 和 val() 返回 null

Ner*_*ros 0 null jquery text

编辑:似乎我没有在我的文本区域上使用“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
Run Code Online (Sandbox Code Playgroud)

这次我确保他们都有身份证。


以下代码生成一个警报框,其中没有任何内容。如果我将其更改为 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);
}
});
Run Code Online (Sandbox Code Playgroud)
<textarea name="cost-1" class="costbox"></textarea>
Run Code Online (Sandbox Code Playgroud)

此代码的想法是在值更改时更新小计。这是拼图的最后一块。

当我在 Chrome 中检查时,所有 HTML 看起来都很正常。我的其余所有代码都工作正常。

这应该与 Modernizer1.7 和 jQuery1.5.1 一起运行,基于最近的 HTML5Boilerplate。

这是相同基本事物的另一个示例 - http://jsbin.com/obeho5/3/edit

这可能很简单,但我已经被困在这里几个小时了,看不到它。

[如果它不是显而易见和简单的事情,我可以发布更多代码]

Jam*_*gne 5

#用于通过 ID 获取,您的元素没有 ID 只是一个名称。

添加 ID 或使用此 ID:

'textarea[name="cost-'+i+'"]'