社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

如何通过jquery在表行中指定要提交的动态输入?

user1995565 • 4 年前 • 88 次点击  

我的目标是在一个表单中动态生成一个包含各种项目的表。每一行都有一个与该行关联的唯一输入值。我希望用户能够点击行中的任何地方(无需提交按钮),并通过一个POST请求发送该输入。我只成功地让表单为我选择的每个项目提交相同的输入值,而不是1、2、3等。我缺少什么?

 $("tr").on('click',function(){
    var id = $(this).attr('value');
    var $form = $('#form1');
    
    $form.find("tr[id^='blah']").val(id);

    $form.submit();
    
  });
<table>
<form method="POST" id="form1" action="action">
      <tr id="blah1" value="1">
         <input name="numberInput" value="1" type="hidden"/>
  		    <td><b>Item 1</b>
  		    <td><b>$5</b></td>
      </tr>
      
      <tr id="blah1" value="2">
         <input name="numberInput" value="2" type="hidden"/>
  		    <td><b>Item 2</b>
  		    <td><b>$8</b></td>
      </tr>
      
      ...
      
</form>
</table>

编辑

我自己想出来的。我在表单中添加了一个唯一的标识符,将表单保存在表中,并对jquery进行了下面的修改。

$("tr").click(function(){
    var uniqueID = $(this).find("td:first-child > input").val();
    $("#formName" + uniqueID).submit();
});
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/37908
 
88 次点击  
文章 [ 2 ]  |  最新文章 4 年前
user1995565
Reply   •   1 楼
user1995565    5 年前

我自己想出来的。我在表单中添加了一个唯一的标识符,将表单保存在表中,并对jquery进行了下面的修改。

$("tr").click(function(){
    var uniqueID = $(this).find("td:first-child > input").val();
    $("#formName" + uniqueID).submit();
});
Deji Ojo
Reply   •   2 楼
Deji Ojo    5 年前

首先,表格不能放在表格内,但可以将整个表格放在表格内。

这样地;

<form method="POST" id="form1" action="action">
  <table>
      <!--table content-->
  </table>      
</form>

其次,将隐藏的输入放在表数据标记中。所以,

<tr id="blah1" value="1">
    <input name="numberInput" value="1" type="hidden"/>
    <td><b>Item 1</b>
    <td><b>$5</b></td>
</tr>

将是

<tr id="blah1" value="1">
     <td>
       <input name="numberInput" value="1" type="hidden"/>
     </td>
     <td><b>Item 1</b></td>
     <td><b>$5</b></td>
 </tr>

第三,我的方法是将伪选择器与名称选择器结合使用。这就是我的意思;

https://jsfiddle.net/DejiO/sfdb53hy/13/