【问题标题】:Close Modal after Submit button / Count all the values of checked checkbox提交按钮后关闭模式/计算选中复选框的所有值
【发布时间】:2016-12-29 15:00:09
【问题描述】:

我是 php、html5 和 css 的新手。希望你能帮我解决我的问题。
我使用 css 对其进行样式设置。这是我的代码:

<form method="post" name="testform" action="">

<a href="#modal"> <!-- when the input textbox was clicked, modal will pop up -->
    <input disabled type="text" name="test" placeholder="test" value="">
</a>

    <div class="modalwrapper" id="modal">   <!-- modal -->
            <div class="modalcontainer">    
                <div class="modalcol1">
                    <label>Test 1</label>
                    <input type="checkbox" name="test_modal[]" value="1">
                    <div class="clear"></div>
                    <label>Test 2</label>
                    <input type="checkbox" name="test_modal[]" value="2">
                    <div class="clear"></div>
                    <label>Test 3</label>
                    <input type="checkbox" name="test_modal[]" value="3">
                    <div class="clear"></div>
                    <label>Test 4</label>
                    <input type="checkbox" name="test_modal[]" value="4">
                    <div class="clear"></div>
                    <label>Test 5</label>
                    <input type="checkbox" name="test_modal[]" value="5">
                    <div class="clear"></div>

                    <div class="savebutton">
                        <input class="btn1" type="submit" value="Submit"/>
                    </div>
                </div>
            </div>
        <div class="overlay"></div>
    </div>      
</form>

这是我的 css 代码。

/* modal layout */
    .modalwrapper {
        top: 0;
        left: 0;
        opacity: 0;
        position: absolute;
        visibility: hidden;
        box-shadow: 0 3px 7px rgba(0,0,0,.25);
        box-sizing: border-box;
        transition: all .4s ease-in-out;
        -webkit-transition: all .4s ease-in-out;
        -moz-transition: all .4s ease-in-out;
    }

    .modalwrapper:target {
        opacity: 1;
        visibility: visible
    }

    .overlay {
        background-color: #000;
        background: rgba(0,0,0,.8);
        height: 100%;
        left: 0;
        position: fixed;
        top: 0;
        width: 100%;
        z-index: 1;
    }

    .modalcontainer {
        display: table;
        background-color: #777;
        position: relative;
        z-index: 100;
        color: #fff;
        padding: 5px;
    }

    .modalcol1 { display: table-cell; }

    .clear { clear: both; }

    .modalwrapper input[type=checkbox] {
        float: right;
        margin-right: 20px;
    }

    .savebutton input[type=submit] {
        float: right;
        background-color: maroon;
        color: #fff;
        border: none;
        padding: 5px 10px;
        margin-top: 5px;
        margin-right: 20px;
    }
    /* modal layout ends here */
  1. 我的问题是当我点击提交时,它并没有关闭模式。 我尝试在父窗口上重定向它,但没有任何反应,它没有关闭。

  2. 我还有另一个问题,关于如何计算使用 PHP 选中了多少复选框。如果选中了 1 个复选框,它将显示它的值,如果选中了 2 个以上,它将回显一个值“MORE”

希望任何人都可以帮助我。 如果我的信息不够,请告诉我,以便我更新。 我想了解更多。提前致谢。

【问题讨论】:

  • 这是引导模式吗?请给我们看一下js代码。 PS:你为什么要在a里面放一个input
  • @phaberest - 我完全不知道如何使用 js。但我使用 css 设计并制作了一个模态。我还在锚标记中输入了一个输入,因为当用户单击禁用的输入框时,会弹出模式。

标签: javascript php jquery css html


【解决方案1】:

确保您确实遵循正确的引导模式的 html 格式。注意数据和角色属性。请注意关闭模式的按钮如何具有这些属性。

<div class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

https://getbootstrap.com/javascript/#modals

【讨论】:

  • 嗨。我的帖子已更新。我不知道助推器。我只是使用了一个 id 来调用模态并使用 css 对其进行样式设置。谢谢你,先生。
  • 你只做了我说的一部分。您没有注意数据属性,即 data-dismiss="modal"。再次浏览 html 示例并相应地复制属性。
  • 嗨。我再次更新了我的帖子。我不知道如何使用引导程序,我只是按照我的样式设置它并制作了一个模态。你能检查我的代码吗?我希望它在单击提交按钮后关闭。非常感谢您,很抱歉占用您的时间。
  • 将 data-dismiss="modal" 添加到您的提交按钮。如:
  • 嗨。我要在我的 head 标签上包含什么库?
【解决方案2】:

要打开和关闭您的模式,您必须使用 JS。我建议您使用 jQuery 库(查看this 文章以了解如何在您的 JS 中使用 jQuery)。有了它,您可以简单地使用 .fadeOut() 关闭模式:

$("#idofyourbutton").click(function(){ $("#idofyourmodal").fadeOut(); });

要计算所有选中的复选框,您可以使用即:

var l = $("input[type='checkbox']:checked").length;

之后,您可以使用if else 设置输入字段的内容。

【讨论】:

  • 嗨。感谢您在这里回答。我的帖子已更新。非常感谢
  • 我想我现在明白你的意思了,请查看我的更新答案。如果可行,请反馈给我。
  • 嗨。我再次更新了我的帖子。我不知道如何使用引导程序,我只是按照我的样式设置它并制作了一个模态。你能检查我的代码吗?我希望在单击提交按钮后关闭它,我想计算检查了多少。我只想先关闭我制作的模态。非常感谢您,很抱歉占用您的时间。
  • 没问题,这里就是这样。不知道你没有使用 Bootstrap。使用 jQuery 查看我的新答案。问问这对你有没有帮助。
  • @Mark Manalo 我的第二个问题提示有用吗?
猜你喜欢
  • 1970-01-01
  • 2015-10-14
  • 2014-05-13
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
相关资源
最近更新 更多