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

时间:2013-03-01 04:44:34

标签: javascript forms twitter-bootstrap radio-button mailchimp

我的流程:

  1. 首先,我使用Bootstrap
  2. 为我的网站设计了一个注册表单
  3. 然后,我使用MailChimp创建了一个注册表单,因此我可以跟踪并联系我的注册
  4. 然后,我生成了嵌入表单HTML标记,并将两个
  5. 组合在一起

    问题:

    破坏表单的部分是Bootstrap单选按钮切换使用input type =“button”和MailChimp表单我试图将其映射到使用type =“radio”。

    如果我不使用type =“radio”,则提交有效,但当我在MailChimp中检查我的订阅者的信息时,我错过了该表单部分的数据。

    如果我使用type =“radio”,当用户尝试使用切换按钮时,表单会过早提交,因此表单基本上会提交,然后才能完成填写表单的其余部分。

    JSFiddle演示问题在这里: http://jsfiddle.net/halsey/qwPzy/9/

    解决方案?

    我知道像this这样的线程,它们说你需要用javascript覆盖这种行为,但我找不到如何做到这一点的例子。

    我确信这很容易,但我无法找到资源来帮助我学习如何解决这个问题。提前感谢您提供的任何帮助。

    脚注

    StackOverflow阻止我在没有嵌入代码的情况下链接到JSFiddle。我鼓励你使用上面的链接,但我在嵌入代码时的破坏尝试如下。为奇怪的格式化道歉,但我在将代码嵌入一个块时遇到问题 - 我是StackOverflow的初学者。

    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链接:

    <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链接:

    <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>
    

2 个答案:

答案 0 :(得分:1)

您是否尝试在表单上放置“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);">

并在您的js代码中:

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

答案 1 :(得分:0)

所以我在Bootstrap Github上找到的这个JSFiddle为我工作:http://jsfiddle.net/charettes/SauLj/

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:

    .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;
}