如何从值列表中提交单个值? [英] How do I submit a single value out of list of values?

查看:49
本文介绍了如何从值列表中提交单个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个杂货项目列表,我通过以下代码在我的html页面上呈现:

 <  表格 >  
< 表格 方法 =' POST' action = / buy >
< tr >
< td > {{item}} < / td >
< td >
//使用购买按钮提交商品的代码
< / td >
< / tr >
< / form >
< span class =code-keyword>< / table >



我希望用户点击与他想要购买的商品相对应的购买按钮(一次只能购买一件)



我已尝试下面的代码,但它将所有项目的列表提交回服务器而不是j所需的价值:



 <   table  >  
< 表单 方法 =' POST' action = / buy >
< tr >
< td > {{item}} < / td > ;
< td >
< 按钮 类型 = 提交 < span class =code-keyword>>

下载
< 输入 类型 = 隐藏 名称 = item_to_buy = {{item}} >
< / button >
< / td >
< / tr >
< / form >
< / table >

解决方案

提交按钮将在激活时始终将所有相关信息(包括所有隐藏字段)发回服务器...

你有两个选择......

将值直接放在submi上t按钮。它不会改变提交行为,但你会知道用户按下哪个按钮...

更好的方法是去学习AJAX方法......你的想法需要它。 ..

I have a list of grocery items which I render on my html page by code below:

<table>
<form method='POST' action="/buy">
    <tr>
        <td>{{item}}</td>
        <td>
            //Code for submitting the item using buy button
        </td>
    </tr>
</form>
</table>


I want user to click on the Buy button corresponding to the item he wants to buy(only one at a time)

I have tried code below but it submits the list of all the items back to the server and not just the value needed:

<table>
<form method='POST' action="/buy">
    <tr>
        <td>{{item}}</td>
        <td>
            <button type="submit">
                Download
                <input type="hidden" name="item_to_buy" value="{{item}}">
            </button>
        </td>
    </tr>
</form>
</table>

解决方案

A submit button will always send back ALL the relevant information (including ALL hidden fields) to the server when activated...
You have two options...
Put the value directly on the submit button. It does not change the submit behavior, but you will know which button pressed by the user...
A much better way is to go and learn about AJAX methods...Your idea calls for it...


这篇关于如何从值列表中提交单个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆