简体   繁体   English

如何提交选定的复选框项目?

[英]How to submit selected checkbox items?

I have a list of items that need to be selected and take an action based on user's request. 我有一个需要选择的项目列表,并根据用户的请求采取措施。 User selects the items and click on one of the btns to do something on the items. 用户选择项目,然后单击其中一个btns对项目进行操作。

My code is as following but I am not sure how to complete it. 我的代码如下,但是我不确定如何完成它。 I believe, need to put them in a form to be submitted or pass the but not sure how to have a form with two submit btns, (if I need to have ). 我认为,需要将它们放在要提交的表单中或通过,但是不确定如何通过两个提交btns来形成表单(如果需要的话)。

 <body>
        <p><b>Shopping cart</b></p>
        <table>
            <tbody>
                <c:forEach items="${mycart.items}" var="item">
                    <tr>
                        <td>
                            <input type="checkbox" name="Items" 
                                                         value="${item.ID}"/>  
                        </td>
                        <td>
                           Name : ${item.name}

                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
        <a href="checkout" onclick="checkout()">checkout</a>
        <a href="delete" onclick="delete()">Delete</a>

you can easily have two <input type="submit" name="something" /> in one <form> if you want to differentiate the actions, just use different name for each submit button 您可以轻松地在一个<form>包含两个<input type="submit" name="something" /> <form>如果要区分操作,只需为每个提交按钮使用不同的name

EDIT: 编辑:

<form ...>
   ...
   ...
   <input id="b1" type="submit" name="edit" value="Edit"/>
   <input id="b2" type="submit" name="delete" value="Delete"/>
</form>

If the form above is submitted by clicking #b1, then your request will contain a parameter named "edit". 如果上面的表单是通过单击#b1提交的,那么您的请求将包含一个名为“ edit”的参数。 If the submit is triggered by #b2, then it will contain "delete". 如果提交是由#b2触发的,则它将包含“删除”。

I think following script might let you obtain what items are checked. 我认为以下脚本可能会让您获得已检查的项目。 With jQuery, you need implement your checkout() like this 使用jQuery,您需要像这样实现checkout()

function checkout() {
    $('input[name="Items"]:checkbox').each(function() {
        if ($(this).attr("checked")) {
            alert($(this).val() + 'is checked');
        } else {
            alert($(this).val() + 'is not checked');
        }
    }
    );     

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM