【问题标题】:How to get the value of an input text inside the div of a nested table?如何在嵌套表的 div 中获取输入文本的值?
【发布时间】:2014-12-03 21:58:27
【问题描述】:

我在下面有嵌套表,其中包含 div 元素和输入。我想在单击按钮时捕获并获取其中一种输入类型文本的值。显然现在,使用我下面的 jquery 代码无法获得一个 class="name" 的输入文本,但第一个输入和 textarea 能够被捕获。那么,当单击按钮时,使用 class= "name" 捕获我的第二个输入文本的值的确切代码是什么。谢谢,下面是详细信息:

JQuery 代码:

 $(function () {
           $('.postrep').click(function () {
               //  e.preventDefault();
               var inputs = $(this).closest('div').find('input'); //finding the inputs inside the div
               var textin = $(this).closest('div').find('textarea'); //finding the textarea inside the div

               var dataObject = {

                   Id: inputs.first().val(), // getting the first input value in the div

                   name: inputs.first('input').next('.name').val(), // How do I get this second input value?

                   reply: textin.first().val() //getting the value of the first textarea in the div
               };
             });
          });

HTML:

<table id="mytable">  

    <tr >
        <td class="tdstyle" >

  <div style="font-weight:bold;">  @Html.DisplayFor(modelItem => item.name) </div> 

   <p>  @Html.DisplayFor(modelItem => item.comment) </p>
  <p > <input type="button" id="like" name="like" value="Like" /> <input type="button" class ="Reply" name="Reply" value="Replie(s)" /></p>                                                                                                                                                                                         
   <div id="divReply" class ="divrep" >
           <table>

                 <tr >

                     <td >
                         <div style="font-weight:bold;"> @Html.DisplayFor(modelItem => item2.name) </div> 

                     <p> @Html.DisplayFor(modelItem => item2.reply)  </p>

                   </td>
                 </tr>

            </table>      

     <div> 
       <div class="editor-field" style="display:none; margin-bottom:5px;margin-top:5px">  
          <input type="text" id="comidvalue" name="id" class="id" value="@Html.DisplayFor(modelItem => item.Id)" />
       </div>

       <br />
       <input type="text" id="namerep" name="name" class="name" />  <!-- I want to get the value of this -->

      <br />
      <textarea id="reply" name="reply" class="reply" ></textarea>  

         <br />

       <input type="button" class="postrep" value="Post Reply" name="butname" style="cursor:pointer" /> 

     </div>
            <br />

  </div>
        </td>       
    </tr>


</table>

【问题讨论】:

    标签: javascript jquery html ajax


    【解决方案1】:

    您可以使用id选择器获取输入框的值

    $('#namerep').val();
    

    但请确保您没有多个输入具有相同的id,如果您有多个ids,则删除 id 并仅使用class="name" 来获取输入值。

    您可以使用 jQuery 属性选择器通过class="name" 获取输入值,请参见下面的代码 -

    $(function () {
               $('.postrep').click(function () {
                   //  e.preventDefault();
                   //store parent div in a variable and use it at multiple places
                   var $parentDiv = $(this).closest('div');
                   var inputs = $parentDiv.find('input'); //finding the inputs inside the div
                   var textin = $parentDiv.find('textarea'); //finding the textarea inside the div
    
                   var dataObject = {
    
                       Id: inputs.first().val(), // getting the first input value in the div
    
                       name: $parentDiv.find('input[class="name"]').val(), //get closest div and find input with class="name"
    
                       reply: textin.first().val() //getting the value of the first textarea in the div
                   };
                 });
              });
    

    【讨论】:

    • 谢谢,应该是名字:$(this).closest('div').find('input[class="name"]').val(),
    • @timmack,对不起我的错 .. 从代码中删除了 ;
    • 你为什么编辑你的答案?无需使您的代码复杂化。这是正确的,只是缺少 .val()
    【解决方案2】:

    你可以得到

    var name= $('#namerep').val();

    【讨论】:

    • 为什么需要closest('div').find('#namerep') 如果$('#namerep') 会发现输入,因为id 必须是唯一的。
    • @BhushanKawadkar:我同意。
    • 不,当元素在内部循环时不能使用 id 而不是类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多