Noob jQuery"$"函数返回问题

Mis*_*iur 0 html javascript jquery object

HTML:

<input type="text" id="priceperperson1" name="priceperperson1" />
<input type="text" name="personsvalue1" class="countme" readonly="readonly" />
Run Code Online (Sandbox Code Playgroud)

JS:

jQuery(document).ready(function($) {
$('div.pricerow input.countme').each(function(){ 
var id = this.name.substr(this.name.length-1);
alert($('input#priceperperson'+id));
this.value = parseInt($('priceperperson'+id).value) * parseInt($('countpersons'+id).value); 
});
});
Run Code Online (Sandbox Code Playgroud)

缩短尽可能缩短.所有我警惕的是"对象"......价值是NaN.我试图在id上"parseInt".我试过了:

$('[name=priceperperson'+id+']');
$('priceperperson'+id);
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

Gab*_*oli 5

您正在检索jQuery对象 $(..)

要获取值(字符串),请使用该.val()方法.

所以

alert( $('input#priceperperson'+id).val() );
Run Code Online (Sandbox Code Playgroud)