【问题标题】:HTML checkbox passing all values insted of selectedHTML复选框传递所有值而不是选中
【发布时间】:2015-12-02 16:33:18
【问题描述】:

我有一个将值传递给 django 视图的表单。
在表单中有一个带有复选框的动态生成的表格。

HTML:

<table class="table table-striped">
    <thead>
        <tr>
            <th><input type="checkbox" onClick="toggle(this, 'no')"></th>
            <th>Order Id</th>
            <th>Channel</th>
            <th>Dispatch By Date</th>
            <th>Amount</th>
            <th>Status</th>
            <th>Products</th>
        </tr>
    </thead>
    <tbody>
        {% for oid,oiid,dbd,stts,tp,sku,qty,chnl in new_orders %}
        <tr>
            <td><input type="checkbox" name="no" value="{{oid}}"></td>
            <td>
                <div><a class="btn-link" href="#">{{oid}}</a></div>
                <div><input name="oiid" style="display:none" value="{{oiid}}">{{oiid}}</div>
            </td>
            <td>{{chnl}}<input name="channel" style="display:none" value="{{chnl}}"></td>
            <td><span class="text-muted"><i class="fa fa-clock-o"></i>&nbsp;&nbsp;&nbsp;{{dbd}}</span></td>
            <td>{{tp}}</td>
            <td>{{stts}}</td>
            <td>
                <div>{{sku}}</div>
                <div>Quantity: {{qty}}</div>
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>

我将 3 个值传递回我的视图。 oid, oiid, chnl。 问题是如果我选择一些元素而不是全部,那么所有 oid 的值都会为这些元素传递,但 oiidchnl 的所有值都会传递。

我的 Javascript:

function toggle(source, text) {
        checkboxes = document.getElementsByName(text);
        for(var i=0, n=checkboxes.length;i<n;i++) {
            checkboxes[i].checked = source.checked;
            }
        }

如何只传递为所有三个变量选择的值?

【问题讨论】:

  • 那么……我的回答对你有帮助吗?

标签: javascript html django


【解决方案1】:

您必须使用type="checkbox"name="YOURCHECKBOXNAME[]" 键入所有输入。

YOURCHECKBOXNAME 应该是一样的。

当您在 HTML 输入中输入 [] 时,表单将向 PHP 发送一个数组。

【讨论】:

  • 对不起,我出去工作了..我应该像这样传递值:
  • 是的。当您使用 POST 发送表单时,您将收到一个数组。
  • 如果您需要更多示例或帮助,请告诉我。但请记住,我的英语说得不太好。我是法国学生,所以... :)
猜你喜欢
  • 1970-01-01
  • 2017-12-27
  • 2016-03-04
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多