简体   繁体   English

请帮助我防止Bootstrap单选按钮过早提交表单(使用Javascript?)

[英]Please help me prevent Bootstrap radio buttons from submitting my form too early (using Javascript?)

My process: 我的过程:

  1. First I designed a signup form for my website using Bootstrap 首先,我使用Bootstrap为我的网站设计了一个注册表单
  2. Then, I created a signup form using MailChimp, so I can track and contact my signups 然后,我使用MailChimp创建了一个注册表单,因此我可以跟踪并联系我的注册
  3. I then generated the embed form HTML markup, and combined the two 然后,我生成了嵌入表单HTML标记,并将两者合并

The issue: 问题:

The part that is breaking the form is that the Bootstrap radio button toggle uses input type="button" and the MailChimp form I am trying to map it to uses type="radio". 破坏表单的部分是Bootstrap单选按钮切换开关使用输入type =“ button”,而我试图将MailChimp表单映射为使用type =“ radio”。

If I don't use type="radio", the submission works but when I check the information on my subscribers in MailChimp, I am missing data from that part of the form on. 如果我不使用type =“ radio”,则提交工作正常,但是当我在MailChimp中检查有关订阅者的信息时,表单中该部分的数据丢失了。

If I use type="radio", the form prematurely submits when the user tries to use the toggle button, so basically the form submits before they can finish filling out the rest of the form. 如果我使用type =“ radio”,则当用户尝试使用切换按钮时,表单会过早提交,因此,基本上,表单会在用户完成填写其余表单之前提交。

JSFiddle Demo of the issue is here: http://jsfiddle.net/halsey/qwPzy/9/ 问题的JSFiddle演示在这里: http : //jsfiddle.net/halsey/qwPzy/9/

The solution? 解决方案?

I am aware of threads like this , which say you will need to override this behavior with javascript, but I can't find an example of how to do this. 我知道像线程的这个 ,它说你需要重写使用JavaScript这种行为,但我无法找到如何做到这一点的例子。

I'm sure this is embarrassingly easy but I am having trouble locating resources to help me learn how to fix this. 我敢肯定这很容易实现,但是我在查找资源以帮助我学习如何解决此问题时遇到了麻烦。 Thanks in advance for any help you can provide. 在此先感谢您提供的任何帮助。

Footnote 脚注

StackOverflow is preventing me from linking to JSFiddle without embedding code. StackOverflow阻止我在不嵌入代码的情况下链接到JSFiddle。 I would encourage you to use the link above, but my broken attempt at embedding the code is below. 我鼓励您使用上面的链接,但是下面嵌入代码的失败尝试。 Apologies for the weird formatting but I am having trouble embedding the code in one chunk - I am a StackOverflow beginner. 不好意思的格式道歉,但我无法将代码嵌入到一个块中-我是StackOverflow初学者。

HTML: HTML:

<form action="http://foorder.us6.list-manage1.com/subscribe/post?u=81c3da3b3ebbf564cf50a78ff&amp;id=f16451f556" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>

                            <fieldset class="mc-field-group">


                                <div class="form-field input-prepend">

                                        <span class="add-on">http://Foorder.com/</span>

                                        <input class="span3 required" id="prependedInput mce-MMERGE2" type="text" placeholder="YourProfile" value="" name="MMERGE2">

                                 </div><!-- / .formfield .input-prepend -->


                                <div class="form-field">


                                       <input type="email" class="span3 required email" placeholder="Email" value="" name="EMAIL" id="mce-EMAIL">



                                    <input type="text" class="span2 required" placeholder="Zip Code" value="" name="MMERGE8" id="mce-MMERGE8">

                                </div><!-- / .formfield -->

                                <div class="form-field">



                                      I want to&nbsp;&nbsp;
                                        <div class="btn-group" data-toggle="buttons-radio">
                                             <button type="radio" class="btn" value="Buy" name="MMERGE3" id="mce-MMERGE3-0">Buy</button>
                                             <button type="radio" class="btn" value="Sell" name="MMERGE3" id="mce-MMERGE3-1">Sell</button>
                                        </div>
                                      &nbsp;&nbsp;items on Foorder

                                </div><!-- / .formfield -->

                                <div class="form-field">

                                        <button type="button" class="btn btn-link" data-toggle="collapse" data-target="#promo-code-toggle">

                                            Have a promo code?

                                         </button>


                                        <div id="promo-code-toggle" class="collapse">

                                                   <input type="text" class="span2" placeholder="Promo Code" value="" name="MMERGE1 promocodeinput" id="mce-MMERGE1 promo-code-input">


                                        </div><!-- / .formfield -->




                                </div><!-- / .formfield -->


                                 <div id="mce-responses" class="clear">

                                        <div class="response" id="mce-error-response" style="display:none"></div>

                                        <div class="response" id="mce-success-response" style="display:none"></div>

                                </div>  


                                <div class="form-field">


                                    <button type="submit" class="button btn btn-primary" value="Subscribe" name="subscribe" id="mc-embedded-subscribe">Submit</button>

                                </div><!-- / .formfield -->


                            </fieldset>

                       </form>

CSS HEAD links: CSS HEAD链接:

<link href="http://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
<link href="http://foorder.com/_Resources/SBX/css/bootstrap.css" rel="stylesheet">
<link href="http://foorder.com/_Resources/SBX/css/bootstrap-responsive.css" rel="stylesheet">

Javascript BODY links: Javascript BODY链接:

<script src="http://foorder.com/_Resources/SBX/js/jquery.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-transition.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-alert.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-modal.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-dropdown.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-scrollspy.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-tab.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-tooltip.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-popover.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-button.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-collapse.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-carousel.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-typeahead.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/bootstrap-affix.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/holder/holder.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/google-code-prettify/prettify.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/application.js"></script>
<script src="http://foorder.com/_Resources/SBX/js/custom.js"></script>

Have you tried putting an "onsubmit" on the form and running thru a function to determine testing? 您是否尝试过在表单上放置“ onsubmit”并通过函数运行以确定测试?

ala

<form action="http://foorder.us6.list-manage1.com/subscribe/post?u=81c3da3b3ebbf564cf50a78ff&amp;
    id=f16451f556" method="post" id="mc-embedded-subscribe-form" 
    name="mc-embedded-subscribe-form" class="validate" target="_blank" 
    onsubmit="return checkValidity(this);">

and in your js code: 并在您的js代码中:

function checkValidity(form){
  if(){
       // return false <-- doesn't submit
      }
  }

So this JSFiddle that I found on the Bootstrap Github worked for me: http://jsfiddle.net/charettes/SauLj/ 因此,我在Bootstrap Github上找到的JSFiddle对我有用: http : //jsfiddle.net/charettes/SauLj/

HTML: HTML:

<div class="btn-group">
<input type="radio" name="radios" id="radio_1" />
<label class="btn" for="radio_1">Radio one</label>
<input type="radio" name="radios" id="radio_2" />
<label class="btn" for="radio_2">Radio two</label>
<input type="radio" name="radios" id="radio_3" />
<label class="btn" for="radio_3">Disabled radio three</label>

CSS: CSS:

    .btn-group > input {
    display: none;
}

.btn-group input:first-child + .btn {
     /* This is an actual copy/paste of the .btn-group .btn:first-child style */
    border-bottom-left-radius: 4px;
    border-top-left-radius: 4px;
    margin-left: 0;
}

input:checked + label.btn {
    /* This is an actual copy/paste of the .btn:active style */
    background-color: #E6E6E6;
    background-image: none;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15) inset, 0 1px 2px rgba(0, 0, 0, 0.05);
    color: rgba(0, 0, 0, 0.5);
    outline: 0 none;
}

input[disabled] + label.btn {
    /* This is an actual copy/paste of the .btn:disabled style */
    background-color: #E6E6E6;
    background-image: none;
    box-shadow: none;
    cursor: default;
    opacity: 0.65;
}

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

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