javascript 从表中获取/存储数据

我试图在单击按钮后根据 id 捕获数据(例如存储它们),以便使用它来将所需的 id 插入到数据库中。编写的 js 没有按预期工作,有什么想法缺少什么吗?谢谢你的支持。


    


<script>  

  function goo() {

    idprod=$("#idprod").val();

    nprod=$("#nprod").val();

    lastprod=$("#lastprod").val();

    solarprod=$("#solarprod").val();

    locprod=$("#locprod").val()

  }

</script>

<form name="goo" method="post"> 

<table border="1">

  <tr>

    <th>ID</th>

    <th>Name</th>

    <th>Lastname</th>

    <th>WK_Solar</th>

    <th>Location</th>

    <th>Save</th>

  </tr>

    <tr>  

    <td id="idprod">P1</td>

    <td id="nprod">James</td>

    <td id="lastprod">Lee</td>

    <td id="solarprod">$1555</td>

    <td id="locprod">Queens</td>

    <td><input type="hidden" name="active" value="active"><input type="submit" name="submit" value="goo"></td>

  </tr>

  <tr>  

    <td id="idprod">P2</td>

    <td id="nprod">Marc</td>

    <td id="lastprod">Hoobs</td>

    <td id="solarprod">$955</td>

    <td id="locprod">Bronx</td>

    <td><input type="hidden" name="active" value="active"><input type="submit" name="submit" value="goo"></td>

  </tr>

</table>

</form>


侃侃无极
浏览 41回答 1
1回答

宝慕林4294392

1- 您正在使用 val,它是属性 Value。您应该使用 html() 或 text() 来获取值。2-您有两行,您应该浏览这两行并从每行获取数据&nbsp;function goo() {&nbsp; &nbsp; var firstRow = $(".test tbody tr").last();&nbsp; &nbsp; idprod=firstRow.find("#idprod").html();&nbsp; &nbsp; nprod=firstRow.find("#nprod").html();&nbsp; &nbsp; lastprod=firstRow.find("#lastprod").html();&nbsp; &nbsp; solarprod=firstRow.find("#solarprod").html();&nbsp; &nbsp; locprod=firstRow.find("#locprod").html()&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; console.log(idprod)&nbsp; }&nbsp; $(document).ready(function(){&nbsp;&nbsp;&nbsp; &nbsp;goo();&nbsp; });&nbsp;<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><form name="goo" method="post">&nbsp;<table border="1" class="test"><thead>&nbsp; <tr>&nbsp; &nbsp; <th>ID</th>&nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; <th>Lastname</th>&nbsp; &nbsp; <th>WK_Solar</th>&nbsp; &nbsp; <th>Location</th>&nbsp; &nbsp; <th>Save</th>&nbsp; </tr></thead>&nbsp; &nbsp; <tr>&nbsp;&nbsp;&nbsp; &nbsp; <td id="idprod">P1</td>&nbsp; &nbsp; <td id="nprod">James</td>&nbsp; &nbsp; <td id="lastprod">Lee</td>&nbsp; &nbsp; <td id="solarprod">$1555</td>&nbsp; &nbsp; <td id="locprod">Queens</td>&nbsp; &nbsp; <td><input type="hidden" name="active" value="active"><input type="submit" name="submit" value="goo"></td>&nbsp; </tr>&nbsp; <tr>&nbsp;&nbsp;&nbsp; &nbsp; <td id="idprod">P2</td>&nbsp; &nbsp; <td id="nprod">Marc</td>&nbsp; &nbsp; <td id="lastprod">Hoobs</td>&nbsp; &nbsp; <td id="solarprod">$955</td>&nbsp; &nbsp; <td id="locprod">Bronx</td>&nbsp; &nbsp; <td><input type="hidden" name="active" value="active"><input type="submit" name="submit" value="goo"></td>&nbsp; </tr></table></form>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5