选中时,从表TD内的输入字段中检索值

时间:2016-07-22 12:30:50

标签: javascript jquery html twitter-bootstrap jsp

我想知道当选择(点击)此TD时,是否有某种方法可以从TD内的输入字段中检索值。

我设法得到了1个值,但是当我按下另一行时它没有改变。

例如,这是我的JSP,它导致2个TD:

               <div class="col-md-6">
                        <table class="table fixed">
                            <thead>
                                <tr>
                                    <th>Choose what account you want to login to</th>
                                    <td align="right"><strong></strong></td>
                                </tr>
                            </thead>
                            <c:forEach var="i" begin="0" end="${roleList.getSize() -1}">
                                <tbody id="extend">
                                    <tr data-toggle="collapse123" class="clickableRow spaceUnder" nowrap="true" data-target=".demo1" id="1">
                                        <td class="AlignLeft" nowrap="true" style="border-right: 0px;">
                                            <label for="important"style="display: inline !important">${roleList.getFirstName(i)} ${roleList.getLastName(i)}</label>
                                        </td>
                                        <td align="right" class="AlignRight" style="border-left: 0px;">
                                            <input type="hidden" id ="roleId" name="chosenRole" value="${roleList.getClientId(i)}">
                                        </td>
                                    </tr> 
                                </c:forEach>

                        </table>
                        <input type="hidden" id ="roleClicked" name="roleClicked" value="">
                    </div>

这是我的javascript / jquery:

$( document ).ready(function() {

            $("#extend td").click(function () {
  

如何选择点击的TD的输入字段值?

                $("#roleClicked").text();
            });
    });
  

编辑:我回来是正确的,虽然我不得不将我的输入字段从第二个TD移动到第一个TD,这对我来说完全没问题(无论如何都是隐藏的)

2 个答案:

答案 0 :(得分:1)

您需要使用Check this link

$(document).ready(function(){    
$('#extend td').click(function(){
    var roleid = $(this).find('input').val();
    $("#roleClicked").val(roleid);
});
});

而不是 - val()方法返回或设置所选元素的value属性。

$("#roleClicked").text();

答案 1 :(得分:0)

解决方案应该是这个

$( document ).ready(function() {

            $("#extend td").click(function () {
                $("#roleClicked").val($(this).find("input").val());
            });
    });

OR用于基于ajax的加载。

$( document ).ready(function() {
    $( "#extend" ).on( "click", "td", function() {
        $("#roleClicked").val($(this).find("input").val());
    });
});
相关问题