2011-11-29 81 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