开发者

jQuery val() undefined

We have the following XHTML table:

  <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>

When there is a click over a TD.gantt element, we want jQuery to get the value from input[name='line_config'] tag. We try the following jQuery code, but val() returned '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());
    });
});

Are we getting right the value way? If anyone can help us, we appreciate... =)


Note jquery.children() will only return direct/immediate children, and since your input is not a direct/immediate child of the <TR> you will not get any of the inputs.

May I suggest something like this

$("input[name='line_config']", tag.closest('tr'))


children return the immediate children of the tr element. You should try by find i.e.:

values = tag.closest('tr').find("input[name='line_config']").val();


Not sure if this is the whole problem, but when you say var gantt_tag = $('td.preop');, there aren't any elements with the class of preop:

$('td.gantt').click(function() {
  var gantt_tag = $('td.preop');
  alert(gantt_tag.size()); // returns 0, i.e. no elements are matched
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜