我有一个动态生成的html

var resultcard = `
    <tr class="tr-shadow">
           <td class="desc">
           <span class="block ">
                  ${Name}
               </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;" />

            <a>UI</a>
        </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>
      <button type="button" class="btn btn-primary btn-md" onclick="postitem(this);">Submit</button>
        </td>
 </tr>

          `
            container.innerHTML += resultcard;

        })

postem函数是这样的
function postitem(data){

console.log(data)


  }

单击提交按钮时,如何获取id“qty”和“price”的输入值?

最佳答案

使用Jquery表单数据:

    $('#myform').serializeArray();
   // this method fetch all form data in array of key and value

function postitem(){
var data=$('#myform').serializeArray();
console.log(data);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <form id="myform">
<tr class="tr-shadow">
           <td class="desc">
           <span class="block ">
                  ${Name}
               </span>
           </td>
         <td class="desc">
          <input class="au-input au-input--sm" id="qty" type="text" name="searchqty" placeholder="i.e. 20 EA" style="width: 100px;" />

            <a>UI</a>
        </td>
       <td class="desc">
       <input class="au-input au-input--sm" id="price"  type="text" name="searchprice" placeholder="i.e 900" style="width: 90px;" />
       </td>
       <td>
          <span class="status--process">
          <input class="au-input au-input--sm" type="text" name="searchreports" placeholder="Search for datas &amp; reports..." style="width: 90px;" />
          </span>
        </td>
      <td class="desc">
      <input class="au-input au-input--sm" type="text" name="searchdatas" placeholder="Search for datas &amp; reports..." style="width: 90px;"  />
       </td>
       <td>
      <button type="button" class="btn btn-primary btn-md" onclick="postitem();">Submit</button>
        </td>
 </tr>
   </form>

关于javascript - 如何获取动态生成的输入字段的输入值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51202343/

10-10 00:36