2011-11-29 87 views
3

我有一个简单的功能,看起来像这样jQuery的解析HTML

var getCellText = function ($cell) { 
    var html = $cell.html(); 
    $(html).find("input").each(function() { 
    alert(this.value); 
    }); 
} 

的HTML可能看起来像这样

<input class="lineno" name="lineno" value="4109375" type="hidden"> 
<input onchange="Time.UpdateLine(this, 'studentno', this.value, '4109375');" class="studentno" value="2088" type="text"> 

我需要得到studentno输入变量的值(我可以忽略名为lineno的输入)。

我怎么能做到这一点,你可以看到,我已经给了它一个尝试

回答

7

你过于复杂的事情。请勿使用.html(),请使用.val()

function getCellText($cell) 
{ 
    var value = $cell.find('input.studentno').val(); 
    console.log(value); 
} 
+1

+1使用.find () –

+0

感谢您的帮助 – The87Boy

1

假设$cell变量是包含的.studentno父元素一个jQuery对象,这将工作:

var getCellText = function ($cell) { 
    alert($(".studentno", $cell).val()); 
} 
0

你可以试试这个:

$('input.studentno',html).each(function() { 
    alert($(this).val()); 
}); 
+1

-1,正确的答案是摆脱'.html()'位。 –

+1

“正确的答案”... –

+0

是的,我不知道你的意思是通过去掉'.html()'位。 – arb