如何从某个HTML ID检索输入

时间:2018-07-10 11:00:07

标签: javascript jquery html

我有这个html

 <div class="table-responsive table-responsive-data2">
<table class="table table-data2" id="resulttable" style="display: block;">
    <thead>
        <tr>

            <th>NSN</th>
            <th>Name</th>
            <th>QTY</th>
            <th>Price ($)</th>
            <th>Manufacturer</th>
            <th>Part Number</th>
            <th>Shipping</th>
        </tr>
    </thead>
    <tbody id="producttable">



        <!--  -->


        <form id="myform0"></form>

        <tr class="tr-shadow">

            <td style="width: 90px;">
                <div>123
                </div>
                <div>
                    <br>
                    <button type="button" class="btn btn-secondary mb-1 btn-sm" data-toggle="modal" data-target="#productModal" onclick="showproductmodal(123)">
                        Add Photos
                    </button>
                </div>

            </td>
            <td>

                <span class="status--process">
                    <input class="au-input au-input--sm" id="Nomenclature" type="text" name="search" placeholder="Search for datas &amp; reports..." style="width: 90px;">
                </span>

            </td>
            <td class="desc">

                <input class="au-input au-input--sm " id="Qty" type="text" name="search" placeholder="i.e. 20 EA" style="width: 100px;">


            </td>
            <td class="desc">

                <input class="au-input au-input--sm" id="Price" type="text" name="search" placeholder="i.e 900" style="width: 90px;">

            </td>

            <td>
                <span class="status--process">
                    <input class="au-input au-input--sm" type="text" name="search" placeholder="Search for datas &amp; reports..." style="width: 90px;">
                </span>

            </td>
            <td class="desc">
                <input class="au-input au-input--sm" type="text" name="search" placeholder="Search for datas &amp; reports..." style="width: 90px;">
            </td>

            <td class="status--process">

                <input class="au-input au-input--sm" type="text" name="search" placeholder="shipping cost" style="width: 120px; height: 30px;"><br>

                <select name="selectSm" id="SelectLm" class="form-control-sm form-control" style="width: 120px;">


                    <option value="0">Please select</option>
                    <option value="1">Option #1</option>
                    <option value="2">Option #2</option>
                    <option value="3">Option #3</option>
                    <option value="4">Option #4</option>
                    <option value="5">Option #5</option>
                </select>
            </td>
            <td>

                <button type="button" class="btn btn-primary btn-md" onclick="postitem(0)">Submit</button>

            </td>
        </tr>


        <tr class="spacer"></tr>


        <form id="myform1"></form>

        <tr class="tr-shadow">

            <td style="width: 90px;">
                <div>343
                </div>
                <div>
                    <br>
                    <button type="button" class="btn btn-secondary mb-1 btn-sm" data-toggle="modal" data-target="#productModal" onclick="showproductmodal(343)">
                        Add Photos
                    </button>
                </div>

            </td>
            <td>

                <span class="status--process">
                    <input class="au-input au-input--sm" id="Nomenclature" type="text" name="search" placeholder="Search for datas &amp; reports..." style="width: 90px;">
                </span>

            </td>
            <td class="desc">

                <input class="au-input au-input--sm " id="Qty" type="text" name="search" placeholder="i.e. 20 EA" style="width: 100px;">


            </td>
            <td class="desc">

                <input class="au-input au-input--sm" id="Price" type="text" name="search" placeholder="i.e 900" style="width: 90px;">

            </td>

            <td>
                <span class="status--process">
                    <input class="au-input au-input--sm" type="text" name="search" placeholder="Search for datas &amp; reports..." style="width: 90px;">
                </span>

            </td>
            <td class="desc">
                <input class="au-input au-input--sm" type="text" name="search" placeholder="Search for datas &amp; reports..." style="width: 90px;">
            </td>

            <td class="status--process">

                <input class="au-input au-input--sm" type="text" name="search" placeholder="shipping cost" style="width: 120px; height: 30px;"><br>

                <select name="selectSm" id="SelectLm" class="form-control-sm form-control" style="width: 120px;">


                    <option value="0">Please select</option>
                    <option value="1">Option #1</option>
                    <option value="2">Option #2</option>
                    <option value="3">Option #3</option>
                    <option value="4">Option #4</option>
                    <option value="5">Option #5</option>
                </select>
            </td>
            <td>

                <button type="button" class="btn btn-primary btn-md" onclick="postitem(1)">Submit</button>

            </td>
        </tr>


        <tr class="spacer"></tr>


        <form id="myform2"></form>

        <tr class="tr-shadow">

            <td style="width: 90px;">
                <div>212
                </div>
                <div>
                    <br>
                    <button type="button" class="btn btn-secondary mb-1 btn-sm" data-toggle="modal" data-target="#productModal" onclick="showproductmodal(212)">
                        Add Photos
                    </button>
                </div>

            </td>
            <td>

                <span class="status--process">
                    <input class="au-input au-input--sm" id="Nomenclature" type="text" name="search" placeholder="Search for datas &amp; reports..." style="width: 90px;">
                </span>

            </td>
            <td class="desc">

                <input class="au-input au-input--sm " id="Qty" type="text" name="search" placeholder="i.e. 20 EA" style="width: 100px;">


            </td>
            <td class="desc">

                <input class="au-input au-input--sm" id="Price" type="text" name="search" placeholder="i.e 900" style="width: 90px;">

            </td>

            <td>
                <span class="status--process">
                    <input class="au-input au-input--sm" type="text" name="search" placeholder="Search for datas &amp; reports..." style="width: 90px;">
                </span>

            </td>
            <td class="desc">
                <input class="au-input au-input--sm" type="text" name="search" placeholder="Search for datas &amp; reports..." style="width: 90px;">
            </td>

            <td class="status--process">

                <input class="au-input au-input--sm" type="text" name="search" placeholder="shipping cost" style="width: 120px; height: 30px;"><br>

                <select name="selectSm" id="SelectLm" class="form-control-sm form-control" style="width: 120px;">


                    <option value="0">Please select</option>
                    <option value="1">Option #1</option>
                    <option value="2">Option #2</option>
                    <option value="3">Option #3</option>
                    <option value="4">Option #4</option>
                    <option value="5">Option #5</option>
                </select>
            </td>
            <td>

                <button type="button" class="btn btn-primary btn-md" onclick="postitem(2)">Submit</button>

            </td>
        </tr>


        <tr class="spacer"></tr>

    </tbody>

</table>

我有一个从动态创建的html触发的函数

function postitem(index) {
   var formid='#myform'+index

    $('input').each(function(index){

     console.log($(this).val()); 
 });
}

选择器$('input')捕获html中的所有输入值,我真正想要的是它仅捕获给定#id内的输入。所以我尝试了

 $('#myform1 input').each(function(index){

     console.log($(this).val()); 
 });

$(formid).('input')

,但没有一个产生任何结果。如何实现在div ID内检索输入?

3 个答案:

答案 0 :(得分:0)

您可以非常直接地访问输入而无需使用父级$("#idInput"),或者可以通过使用$('#myform1 #idInput')来通过父级访问输入。但是,请确保在生成时已定义元素的ID。

还有另一种方法可以将其用作数组$('#myform1 input')[index]。请注意,索引应该是您输入的所需外观数。

答案 1 :(得分:0)

尝试一下:


    $("input").parent( "#myform1" );

答案 2 :(得分:0)

正如我在评论中所说,您的JavaScript代码是正确的。但是有两个问题导致您没有任何结果:

  1. #myform1为空白。

  2. 在另一种形式中,您有一些input控件,但它们没有任何价值。

下面,我在表单#myform1中添加了具有一定值的输入并测试了代码:

$('#myform1 input').each(function(index){

     console.log($(this).val()); 
});
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<form id="myform1"><input value="Some Thing" /></form>