jQuery val()未定义

bet*_*car 2 javascript css jquery jquery-selectors

我们有以下XHTML表:

  <tr class="encabezado">
    <th scope="col" width="2%">1</th>
    <th scope="col" width="2%">2</th>
    <th scope="col" width="2%">3</th>
    <th scope="col" width="2%">4</th>
    <th scope="col" width="2%">5</th>
    <th scope="col" width="2%">...</th>
    <th scope="col" width="2%">31</th>
  </tr>
  <tr>
    <th scope="row">Area 1<input name="line_config" type="hidden" value="0,5,50" /></th>
    <td class="gantt"> </td>
    <td class="gantt"> </td>
    <td class="gantt"> </td>
    <td class="gantt"> </td>
    <td class="gantt"> </td>
    <td class="gantt">...</td>
    <td class="gantt"> </td>
  </tr>
  <tr>
    <th scope="row">Area 2 <input name="line_config" type="hidden" value="0,0,10" /></th>
    <td class="gantt"> </td>
    <td class="gantt"> </td>
    <td class="gantt"> </td>
    <td class="gantt"> </td>
    <td class="gantt"> </td>
    <td class="gantt">...</td>
    <td class="gantt"> </td>
  </tr>
Run Code Online (Sandbox Code Playgroud)

当点击TD.gantt元素时,我们希望jQuery从input [name ='line_config']标记中获取值.我们尝试以下jQuery代码,但val()返回'undefined':

$(document).ready(function() {
    function sum_day(tag, status, column) {
        var total_day = 0;
        var index_pos = 0;
        var values = 0;

        values = tag.closest('tr').children("input[name='line_config']").val();
        alert(values); //Return undefined

        return total_day;
    }

    $('td.gantt').click(function() {
        var gantt_tag = $('td.preop');

        $(this).toggleClass('preop');
        sum_day(gantt_tag, 'preop', $(this).index());
    });
});
Run Code Online (Sandbox Code Playgroud)

我们是否正确的价值方式?如果有人可以帮助我们,我们感谢...... =)

Joh*_*ock 6

注意jquery.children()只会返回直接/直接子节点,并且由于您的输入不是直接/直接子节点,因此<TR> 您将无法获得任何输入.

我可以建议这样的事情

$("input[name='line_config']", tag.closest('tr'))
Run Code Online (Sandbox Code Playgroud)