jQuery解析HTML

时间:2011-11-29 14:14:58

标签: 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 的输入)。

我怎么能这样做,正如你所看到的,我已经试过了

3 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

你可以试试这个:

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