简体   繁体   English

如何使用 Javascript 和 HTML 对多行执行带有按钮的表单操作?

[英]How can I perform a form action with a button next to it for multiple rows using Javascript and HTML?

So, I have a html table in which I have some rows.所以,我有一个 html 表,其中有一些行。 Additional rows are added each time user buys something.每次用户购买东西时都会添加额外的行。 For each row, there is a corresponding input form followed by Buy and Sell buttons.对于每一行,都有一个相应的输入表单,后面是买入和卖出按钮。

What I want to achieve is that, if user wants to buy or sell something once again he can type in the desired quantity and perform his/her action by simply clicking on the buy or sell button from the index page.我想要实现的是,如果用户想再次购买或出售某些东西,他可以输入所需的数量并通过简单地单击索引页面上的购买或出售按钮来执行他/她的操作。 However, there are also Buy and Sell pages from where user also can perform his/her desired actions.但是,也有购买和销售页面,用户也可以从中执行他/她想要的操作。 I am using the form actions I used at Buy and Sell pages inside the index page.我正在使用我在索引页面内的买入和卖出页面中使用的表单操作。

Currently, it works only for the first row in the table.目前,它仅适用于表中的第一行。 If user fills any other row than the first one and clicks buy or sell input form value returns null.如果用户填写除第一行之外的任何其他行并单击买入或卖出输入表单值,则返回 null。

The questions I already looked into:我已经研究过的问题:

Multiple rows have multiple submit buttons, should I make a form for each button? 多行有多个提交按钮,我应该为每个按钮制作一个表单吗?

How do I implement Multiple Submit Buttons for a multi-row form? 如何为多行表单实现多个提交按钮?

Multiple submit buttons in an HTML form HTML 表单中的多个提交按钮

This is how my site looks like: Index Page这是我的网站的样子:索引页面

This is my index.html:这是我的索引。html:

{% extends "layout.html" %}

{% block title %}
Summary
{% endblock %}

{% block main %}
<div id="alertId" class="alert alert-warning alert-dismissible fade show" role="alert" style="display:none">
    <p id="alertContentId"></p>
  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
  </button>
</div>

<div class="table-responsive">
    <table class="table table-striped">
        <thead>
        <tr>
          <th scope="col">Symbol</th>
          <th scope="col">Shares</th>
          <th scope="col">Price</th>
          <th scope="col">Total</th>
          <th></th>
        </tr>
        </thead>
        <tbody>
        {% for stock in ownings %}
        <tr>
            <td>{{ stock["symbol"].upper() }}</td>
            <td>{{ stock["share_amount"] }}</td>
            <td>{{ stock["price"] | usd }}</td>
            <td>{{ stock["stock_total"] | usd }}</td>
            <td>
                <form id="action_form" method="post">
                    <span class="form-group">
                        <input id="share_quantity" autocomplete="off" autofocus class="form-control" name="shares" placeholder="Shares" type="number" min="1">
                        <input value="{{ stock["symbol"] }}" type="hidden" name="symbol">
                    </span>
                    <button id="btn_buy" class="btn btn-primary" type="submit" data-action="buy">Buy</button>
                    <button id="btn_sell" class="btn btn-primary" type="submit" data-action="sell">Sell</button>
                </form>
            </td>
        </tr>
        {% endfor %}
        <tr>
            <td>CASH</td>
            <td></td>
            <td></td>
            <td>{{ cash | usd }}</td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td><strong>{{ grand_total | usd}}</strong></td>
            <td></td>
        </tr>
        </tbody>
    </table>
</div>
<script src="{{url_for('static', filename='button.js')}}"></script>
{% endblock %}

This is my buy.html:这是我买的。html:

{% extends "layout.html" %}

{% block title %}
Buy
{% endblock %}

{% block main %}
<form action="/buy" method="post">
    <div class="form-group">
        <input autocomplete="off" autofocus class="form-control" name="symbol" placeholder="Symbol" type="text">
    </div>
    <div class="form-group">
        <input autocomplete="off" autofocus class="form-control" name="shares" placeholder="Shares" type="number" min="1">
    </div>
    <button class="btn btn-primary" type="submit">Buy</button>
</form>
{% endblock %}

This is my sell.html:这是我卖的。html:

{% extends "layout.html" %}

{% block title %}
Sell
{% endblock %}

{% block main %}
<form action="/sell" method="post">
    <div class="form-group">
        <select class="form-control" name="symbol">
            <option disabled selected value="">Symbol</option>
            {% for symbol in available_symbols %}
            <option value="{{ symbol["symbol"] }}">{{ symbol["symbol"].upper() }}</option>
            {% endfor %}
        </select>
    </div>
    <div class="form-group">
        <input autocomplete="off" class="form-control" min="0" name="shares" placeholder="Shares" type="number">
    </div>
    <button class="btn btn-primary" type="submit">Sell</button>
</form>
{% endblock %}

This is my button.js where I try to perform the action of the user depending on the button he/she clicks, for example if user clicks buy button then I add "/buy" action to form.这是我的 button.js,我尝试根据他/她单击的按钮执行用户的操作,例如,如果用户单击购买按钮,那么我将“/购买”操作添加到表单中。

$(document).ready(function() {
$("button").click(function() {
    var action = $(this).data('action'); // get button's action
    // var share_quantity = document.getElementById("share_quantity").value;

    if (document.getElementById("share_quantity").value == "") {
        alert("enter a valid  value");
    }

    if (action == "sell") {
            document.getElementById("action_form").action = "/sell"; // change form's action to sell
            // document.getElementById("alertId").style.display = "block";
            // document.getElementById("alertContentId").innerHTML = "You sold " +share_quantity + " amount of shares!";
    } else if (action == "buy") {
            document.getElementById("action_form").action = "/buy"; // change form's action to buy
    }
});
});

The problem is that all inputs, forms etc have the same ID's;问题是所有输入 forms 等都有相同的 ID; the DOM doesn't like this. DOM 不喜欢这样。 Because getElementById finds multiple values but can only return one, it just takes the first one (which is incorrect for all but the first rows).因为getElementById找到多个值但只能返回一个,所以它只取第一个(除了第一行之外的所有值都不正确)。

I recommend putting some unique ID ( stock["id"] ?) and either adding a data-id attribute with that value and selecting the correct objects based on that, or extracting that value from the name itself (you have access to the button and thus to the button ID).我建议放置一些唯一 ID ( stock["id"] ?) 并添加具有该值的data-id属性并基于该值选择正确的对象,或者从名称本身中提取该值(您可以访问按钮因此到按钮 ID)。 You would need to slightly change your code (not quite sure what the jQuery selector for startswith is, but it does exist) but that shouldn't be hard.您需要稍微更改您的代码(不太确定用于startswith 的jQuery 选择器是什么,但它确实存在),但这应该不难。

Alternatively (though I discourage it) you could use the data from the event to find the parent form of the button clicked and extract values from there.或者(尽管我不鼓励)您可以使用事件中的数据来查找单击按钮的父窗体并从中提取值。 This is a lot harder to debug and maintain.这很难调试和维护。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用php以html形式执行多项操作? - how to perform multiple action in html form using php? 我如何将javascript代码与html表单合并,以便在我们执行诸如“提交”之类的操作并提供输出时触发? - How can I merge javascript code with html form that triggers when we perform some action like “submit” and gives output? 如何使用HTML或javascript将变量传递到action属性以进行下载(按钮/链接), - How can i pass a variable into action attribute for download (button/link) using HTML or javascript, 如何在不使用操作的情况下将action =“ /#”添加到按钮 <form> ? - How can I add action = “/#” to a button without using <form>? Javascript:如何通过单击按钮执行操作? - Javascript: How do I perform a action with the click of a button? 如何使用JavaScript在HTML表单上执行表单验证? - How to perform form validation on HTML form using JavaScript? 如何从具有多行的HTML表单动态地向数据库插入多行? - How Can I Insert Multiple Rows Into a DB from my HTML Form with Multiple Rows Dynamically? 如何对javascript事件执行Html.Action? - How to perform Html.Action on javascript event? 如何制作一个html按钮来执行javascript切换? - How do I make an html button to perform a javascript toggle? 如何忽略 HTML 表单中的禁用问题,以便我能够使用 HTML/JS 应用程序中的“下一步”按钮进入下一部分? - How to ignore disable question in HTML form so I will able to move in next section using "Next" button in HTML/JS application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM