html - 仅当至少选中一个复选框时才使用外部提交按钮提交表单

标签 html jquery css forms

我正在制作一个添加到购物车的程序,但我想将特定产品添加到购物车,其中包含他们选择的属性,例如尺寸和颜色。由于我的提交按钮不在表单内,只有在尺寸表单中至少选中一个复选框时,我才需要使用 Jquery 提交表单,否则单击购物车按钮时它应该显示 div #tipdivcontent .我不明白我哪里出错了。

已更新 FIDDLE 链接,因为插入了错误的链接

下面给出了代码片段和 JSFIDDLE

$(document).ready(function () {
    $('.cartorcheckoutbutton').click(function () {
        var $form = $('#sizesform');
        var $checkbox = $('.roomselect');

        $form.on('submit', function (e) {
            if (!$checkbox.is(':checked')) {
                $('#tipdivcontent').css("display", "block");
                e.preventDefault();
            }
        });
    });
});
#tipdivcontent
{
    display:none;
}
<input type="submit" value="Cart" class="cartorcheckoutbutton">
<form action="../cart" method="POST" id="sizesform">
    <table border="1" cellspacing="0">
        <tr>
            <th colspan="4">Sizes</th>
        </tr>
        <tr>
            <td>
                <label for="2.2">2.2</label>
            </td>
            <td>
                <input class="roomselect" type="checkbox" id="2.2" name="size" value="twopointtwo">
            </td>
            <td>
                <label for="2.4">2.4</label>
            </td>
            <td>
                <input class="roomselect" type="checkbox" id="2.4" name="size" value="twopointfour">
            </td>
        </tr>
        <tr>
            <td>
                <label for="2.6">2.6</label>
            </td>
            <td>
                <input class="roomselect" type="checkbox" id="2.6" name="size" value="twopointsix">
            </td>
            <td>
                <label for="2.8">2.8</label>
            </td>
            <td>
                <input class="roomselect" type="checkbox" id="2.8" name="size" value="twopointeight">
            </td>
        </tr>
        <tr>
            <td colspan="3" align="center">
            <label for="2.10">2.10</label>
            </td>
            <td>
                <input class="roomselect" type="checkbox" id="2.10" name="size" value="twopointten">
            </td>
        </tr>
    </table>
</form>
<div id="tipdivcontent">Please Select Size.</div>

最佳答案

尝试像这样提交您的表单。

我们也可以通过这种方式触发表单的提交事件。

$(document).ready(function () {
            $('.cartorcheckoutbutton').click(function (e) {
                var $form = $('#sizesform');
                var $checkbox = $('.roomselect');
                if (!$checkbox.is(':checked')) {
                    alert('Please confirm!');
                    $('#tipdivcontent').css("display", "block");
                    e.preventDefault();
                }
                else
                    $form.submit();
                
            });            
        });
#tipdivcontent
{
    display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="submit" value="Cart" class="cartorcheckoutbutton">
<form action="../cart" method="POST" id="sizesform">
    <table border="1" cellspacing="0">
        <tr>
            <th colspan="4">Sizes</th>
        </tr>
        <tr>
            <td>
                <label for="2.2">2.2</label>
            </td>
            <td>
                <input class="roomselect" type="checkbox" id="2.2" name="size" value="twopointtwo">
            </td>
            <td>
                <label for="2.4">2.4</label>
            </td>
            <td>
                <input class="roomselect" type="checkbox" id="2.4" name="size" value="twopointfour">
            </td>
        </tr>
        <tr>
            <td>
                <label for="2.6">2.6</label>
            </td>
            <td>
                <input class="roomselect" type="checkbox" id="2.6" name="size" value="twopointsix">
            </td>
            <td>
                <label for="2.8">2.8</label>
            </td>
            <td>
                <input class="roomselect" type="checkbox" id="2.8" name="size" value="twopointeight">
            </td>
        </tr>
        <tr>
            <td colspan="3" align="center">
                <label for="2.10">2.10</label>
            </td>
            <td>
                <input class="roomselect" type="checkbox" id="2.10" name="size" value="twopointten">
            </td>
        </tr>
    </table>
</form>
<div id="tipdivcontent">Please Select Size.</div>

关于html - 仅当至少选中一个复选框时才使用外部提交按钮提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31429268/

相关文章:

jquery - Visual Studio 2015 jQuery 智能感知不工作

html - 自定义 CSS 类在 WordPress 中不起作用

css - Bootstrap-Select 不在所选选项旁边显示刻度(多个)

html - 在 Wordpress 中对齐中心 Aweber 表单

javascript - 为什么我不能单击导航栏的 anchor 标记?

jquery - 删除 jQuery DataTables 中的排序箭头

Javascript 图像 onclick

php - 错误: Extra content at the end of the document Below is a rendering of the page up to the first error

html - 使用 bootstrap 将响应图像对齐到 div 的底部

javascript - 将DIV元素属性的绑定(bind)拖动到knockoutjs中的对象属性