html – Bootstrap模式框中的关闭按钮不起作用

我有一个小的js脚本,在一个人注册时事通讯后,将按钮中的“订阅”更改为“关闭”.我不能在功能上按下按钮,因此模态框实际上正在关闭.

See example page here.

我正在使用的小JS脚本看起来像这样:

<script>
let $input = document.getElementById('mc-embedded-subscribe')
  $input.addEventListener('click', function (){
    this.value= "close"
    this.backgroundColor= "red"
  })
</script>

我在这里错过了什么?

Codepen.

<script type='text/javascript' src='//s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js'></script><script type='text/javascript'>(function($) {window.fnames = new Array(); window.ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='FNAME';ftypes[1]='text';fnames[2]='COUNTRY';ftypes[2]='text';fnames[3]='MMERGE3';ftypes[3]='text';}(jQuery));var $mcj = jQuery.noConflict(true);</script>
								<!--End mc_embed_signup-->
<!-- Start mc_embed Script Signup -->
<script type='text/javascript' src='https://s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js'></script><script type='text/javascript'>(function($) {window.fnames = new Array(); window.ftypes = new Array();fnames[1]='FNAME';ftypes[1]='text';fnames[0]='EMAIL';ftypes[0]='email';
$.extend($.validator.messages, {
  required: "Dette felt er påkrævet.",
  maxlength: $.validator.format("Indtast højst {0} tegn."),
  minlength: $.validator.format("Indtast mindst {0} tegn."),
  rangelength: $.validator.format("Indtast mindst {0} og højst {1} tegn."),
  email: "Indtast en gyldig email-adresse.",
});}(jQuery));var $mcj = jQuery.noConflict(true);
</script>
<script>
let $input = document.getElementById('mc-embedded-subscribe')
  $input.addEventListener('click', function (){
    this.value= "close"
    this.backgroundColor= "red"
  })
</script>
  #mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; }
  /* MailChimp Form Embed Code - Classic - 12/17/2015 v10.7 */
#mc_embed_signup form {display:block; position:relative; text-align:left; padding:10px 0 10px 3%}
#mc_embed_signup h2 {font-weight:bold; padding:0; margin:15px 0; font-size:1.4em;}
#mc_embed_signup input {border: 1px solid #ABB0B2; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;}
#mc_embed_signup input:focus {border-color:#333;}
#mc_embed_signup .button {clear:both; background-color: #aaa; border: 0 none; border-radius:4px; transition: all 0.23s ease-in-out 0s; color: #FFFFFF; cursor: pointer; display: inline-block; font-size:15px; font-weight: normal; height: 32px; line-height: 32px; margin: 0 5px 10px 0; padding: 0 22px; text-align: center; text-decoration: none; vertical-align: top; white-space: nowrap; width: auto;}
#mc_embed_signup .button:hover {background-color:#777;}
#mc_embed_signup .small-meta {font-size: 11px;}
#mc_embed_signup .nowrap {white-space:nowrap;}

#mc_embed_signup .mc-field-group {clear:left; position:relative; width:96%; padding-bottom:3%; min-height:50px;}
#mc_embed_signup .size1of2 {clear:none; float:left; display:inline-block; width:46%; margin-right:4%;}
* html #mc_embed_signup .size1of2 {margin-right:2%; /* Fix for IE6 double margins. */}
#mc_embed_signup .mc-field-group label {display:block; margin-bottom:3px;}
#mc_embed_signup .mc-field-group input {display:block; width:100%; padding:8px 0; text-indent:2%;}
#mc_embed_signup .mc-field-group select {display:inline-block; width:99%; padding:5px 0; margin-bottom:2px;}

#mc_embed_signup .datefield, #mc_embed_signup .phonefield-us{padding:5px 0;}
#mc_embed_signup .datefield input, #mc_embed_signup .phonefield-us input{display:inline; width:60px; margin:0 2px; letter-spacing:1px; text-align:center; padding:5px 0 2px 0;}
#mc_embed_signup .phonefield-us .phonearea input, #mc_embed_signup .phonefield-us .phonedetail1 input{width:40px;}
#mc_embed_signup .datefield .monthfield input, #mc_embed_signup .datefield .dayfield input{width:30px;}
#mc_embed_signup .datefield label, #mc_embed_signup .phonefield-us label{display:none;}

#mc_embed_signup .indicates-required {text-align:right; font-size:11px; margin-right:4%;}
#mc_embed_signup .asterisk {color:#e85c41; font-size:150%; font-weight:normal; position:relative; top:5px;}     
#mc_embed_signup .clear {clear:both;}

#mc_embed_signup .mc-field-group.input-group ul {margin:0; padding:5px 0; list-style:none;}
#mc_embed_signup .mc-field-group.input-group ul li {display:block; padding:3px 0; margin:0;}
#mc_embed_signup .mc-field-group.input-group label {display:inline;}
#mc_embed_signup .mc-field-group.input-group input {display:inline; width:auto; border:none;}

#mc_embed_signup div#mce-responses {float:left; top:-1.4em; padding:0em .5em 0em .5em; overflow:hidden; width:90%; margin: 0 5%; clear: both;}
#mc_embed_signup div.response {margin:1em 0; padding:1em .5em .5em 0; font-weight:bold; float:left; top:-1.5em; z-index:1; width:80%;}
#mc_embed_signup #mce-error-response {display:none;}
#mc_embed_signup #mce-success-response {color:#529214; display:none;}
#mc_embed_signup label.error {display:block; float:none; width:auto; margin-left:1.05em; text-align:left; padding:.5em 0;}

#mc-embedded-subscribe {clear:both; width:auto; display:block; margin:1em 0 1em 5%;}
#mc_embed_signup #num-subscribers {font-size:1.1em;}
#mc_embed_signup #num-subscribers span {padding:.5em; border:1px solid #ccc; margin-right:.5em; font-weight:bold;}

#mc_embed_signup #mc-embedded-subscribe-form div.mce_inline_error {display:inline-block; margin:2px 0 1em 0; padding:5px 10px; background-color:rgba(255,255,255,0.85); -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; font-size:14px; font-weight:normal; z-index:1; color:#e85c41;}
#mc_embed_signup #mc-embedded-subscribe-form input.mce_inline_error {border:2px solid #e85c41;}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


</head>
<body>
<div class="mk row">
    <a href="#">
        <div class="col-sm-12 margin_bottom">
            <div class="hover11 column">
                <figure>
                    <a href="#" data-toggle="modal" data-target="#nyhedsbrev-tilmelding">
                      <img src="https://dohanews.co/wp-content/uploads/2016/07/headline.png" alt="Sign up for our newsletter" class="img-responsive"></img>
                    </a>
                </figure>
            </div>
            <div class="modal fade" id="nyhedsbrev-tilmelding" role="dialog">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-body">
                            <div id="mc_embed_signup">
                                  <form action="https://zalis.us17.list-manage.com/subscribe/post?u=3be651219436ee0f217f040a5&amp;id=d07c036d2e" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
                                    <div id="mc_embed_signup_scroll">
                              	     <h2>Subscribe to our mailing list</h2>
                                      <div class="indicates-required"><span class="asterisk">*</span> indicates required</div>
                                      <div class="mc-field-group">
                              	         <label for="mce-EMAIL">Email Address  <span class="asterisk">*</span></label>
                            	           <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
                                      </div>
                                      <div class="mc-field-group">
                                      	<label for="mce-FNAME">First Name </label>
                                      	<input type="text" value="" name="FNAME" class="" id="mce-FNAME">
                                      </div>
                              	       <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 style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_3be651219436ee0f217f040a5_d07c036d2e" tabindex="-1" value=""></div>
                                      <div class="clear">
                                          <input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button">
                                      </div>
                                  </div>
                              </form>
                            </div>
                        </div>
                        
                    </div>
                </div>
            </div>
            <div class="inner-wrapper bottom-left"></div>
        </div>
    </a>
</div>
</body>
</html>

最佳答案 你可以使用下面的代码,这是一个快速修复.

  1. I have placed a check for checking success message is displayed or not
  2. Timeout of 1 second to change the close text and adding the attribute of data-dismiss of the modal in order to close the popup
let $input = document.getElementById('mc-embedded-subscribe')
  $input.addEventListener('click', function (){
  setTimeout(function(){
  if(document.getElementById("mce-success-response").style.display != "none") {
    document.getElementById('mc-embedded-subscribe').value= "close",
    document.getElementById('mc-embedded-subscribe').setAttribute('data-dismiss','modal')
    }},1000);
  })
<script type='text/javascript' src='//s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js'></script><script type='text/javascript'>(function($) {window.fnames = new Array(); window.ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='FNAME';ftypes[1]='text';fnames[2]='COUNTRY';ftypes[2]='text';fnames[3]='MMERGE3';ftypes[3]='text';}(jQuery));var $mcj = jQuery.noConflict(true);</script>
								<!--End mc_embed_signup-->
<!-- Start mc_embed Script Signup -->
<script type='text/javascript' src='https://s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js'></script><script type='text/javascript'>(function($) {window.fnames = new Array(); window.ftypes = new Array();fnames[1]='FNAME';ftypes[1]='text';fnames[0]='EMAIL';ftypes[0]='email';
$.extend($.validator.messages, {
  required: "Dette felt er påkrævet.",
  maxlength: $.validator.format("Indtast højst {0} tegn."),
  minlength: $.validator.format("Indtast mindst {0} tegn."),
  rangelength: $.validator.format("Indtast mindst {0} og højst {1} tegn."),
  email: "Indtast en gyldig email-adresse.",
});}(jQuery));var $mcj = jQuery.noConflict(true);
</script>
<script>
let $input = document.getElementById('mc-embedded-subscribe')
  $input.addEventListener('click', function (){
  setTimeout(function(){
  if(document.getElementById("mce-success-response").style.display != "none") {
    document.getElementById('mc-embedded-subscribe').value= "close",
    document.getElementById('mc-embedded-subscribe').setAttribute('data-dismiss','modal')
    }},1000);

  })
</script>
#mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; }
  /* MailChimp Form Embed Code - Classic - 12/17/2015 v10.7 */
#mc_embed_signup form {display:block; position:relative; text-align:left; padding:10px 0 10px 3%}
#mc_embed_signup h2 {font-weight:bold; padding:0; margin:15px 0; font-size:1.4em;}
#mc_embed_signup input {border: 1px solid #ABB0B2; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;}
#mc_embed_signup input:focus {border-color:#333;}
#mc_embed_signup .button {clear:both; background-color: #aaa; border: 0 none; border-radius:4px; transition: all 0.23s ease-in-out 0s; color: #FFFFFF; cursor: pointer; display: inline-block; font-size:15px; font-weight: normal; height: 32px; line-height: 32px; margin: 0 5px 10px 0; padding: 0 22px; text-align: center; text-decoration: none; vertical-align: top; white-space: nowrap; width: auto;}
#mc_embed_signup .button:hover {background-color:#777;}
#mc_embed_signup .small-meta {font-size: 11px;}
#mc_embed_signup .nowrap {white-space:nowrap;}

#mc_embed_signup .mc-field-group {clear:left; position:relative; width:96%; padding-bottom:3%; min-height:50px;}
#mc_embed_signup .size1of2 {clear:none; float:left; display:inline-block; width:46%; margin-right:4%;}
* html #mc_embed_signup .size1of2 {margin-right:2%; /* Fix for IE6 double margins. */}
#mc_embed_signup .mc-field-group label {display:block; margin-bottom:3px;}
#mc_embed_signup .mc-field-group input {display:block; width:100%; padding:8px 0; text-indent:2%;}
#mc_embed_signup .mc-field-group select {display:inline-block; width:99%; padding:5px 0; margin-bottom:2px;}

#mc_embed_signup .datefield, #mc_embed_signup .phonefield-us{padding:5px 0;}
#mc_embed_signup .datefield input, #mc_embed_signup .phonefield-us input{display:inline; width:60px; margin:0 2px; letter-spacing:1px; text-align:center; padding:5px 0 2px 0;}
#mc_embed_signup .phonefield-us .phonearea input, #mc_embed_signup .phonefield-us .phonedetail1 input{width:40px;}
#mc_embed_signup .datefield .monthfield input, #mc_embed_signup .datefield .dayfield input{width:30px;}
#mc_embed_signup .datefield label, #mc_embed_signup .phonefield-us label{display:none;}

#mc_embed_signup .indicates-required {text-align:right; font-size:11px; margin-right:4%;}
#mc_embed_signup .asterisk {color:#e85c41; font-size:150%; font-weight:normal; position:relative; top:5px;}     
#mc_embed_signup .clear {clear:both;}

#mc_embed_signup .mc-field-group.input-group ul {margin:0; padding:5px 0; list-style:none;}
#mc_embed_signup .mc-field-group.input-group ul li {display:block; padding:3px 0; margin:0;}
#mc_embed_signup .mc-field-group.input-group label {display:inline;}
#mc_embed_signup .mc-field-group.input-group input {display:inline; width:auto; border:none;}

#mc_embed_signup div#mce-responses {float:left; top:-1.4em; padding:0em .5em 0em .5em; overflow:hidden; width:90%; margin: 0 5%; clear: both;}
#mc_embed_signup div.response {margin:1em 0; padding:1em .5em .5em 0; font-weight:bold; float:left; top:-1.5em; z-index:1; width:80%;}
#mc_embed_signup #mce-error-response {display:none;}
#mc_embed_signup #mce-success-response {color:#529214; display:none;}
#mc_embed_signup label.error {display:block; float:none; width:auto; margin-left:1.05em; text-align:left; padding:.5em 0;}

#mc-embedded-subscribe {clear:both; width:auto; display:block; margin:1em 0 1em 5%;}
#mc_embed_signup #num-subscribers {font-size:1.1em;}
#mc_embed_signup #num-subscribers span {padding:.5em; border:1px solid #ccc; margin-right:.5em; font-weight:bold;}

#mc_embed_signup #mc-embedded-subscribe-form div.mce_inline_error {display:inline-block; margin:2px 0 1em 0; padding:5px 10px; background-color:rgba(255,255,255,0.85); -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; font-size:14px; font-weight:normal; z-index:1; color:#e85c41;}
#mc_embed_signup #mc-embedded-subscribe-form input.mce_inline_error {border:2px solid #e85c41;}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


</head>
<body>
<div class="mk row">
    <a href="#">
        <div class="col-sm-12 margin_bottom">
            <div class="hover11 column">
                <figure>
                    <a href="#" data-toggle="modal" data-target="#nyhedsbrev-tilmelding">
                      <img src="https://dohanews.co/wp-content/uploads/2016/07/headline.png" alt="Sign up for our newsletter" class="img-responsive"></img>
                    </a>
                </figure>
            </div>
            <div class="modal fade" id="nyhedsbrev-tilmelding" role="dialog">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-body">
                            <div id="mc_embed_signup">
                                  <form action="https://zalis.us17.list-manage.com/subscribe/post?u=3be651219436ee0f217f040a5&amp;id=d07c036d2e" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
                                    <div id="mc_embed_signup_scroll">
                              	     <h2>Subscribe to our mailing list</h2>
                                      <div class="indicates-required"><span class="asterisk">*</span> indicates required</div>
                                      <div class="mc-field-group">
                              	         <label for="mce-EMAIL">Email Address  <span class="asterisk">*</span></label>
                            	           <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
                                      </div>
                                      <div class="mc-field-group">
                                      	<label for="mce-FNAME">First Name </label>
                                      	<input type="text" value="" name="FNAME" class="" id="mce-FNAME">
                                      </div>
                              	       <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 style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_3be651219436ee0f217f040a5_d07c036d2e" tabindex="-1" value=""></div>
                                      <div class="clear">
                                          <input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button">
                                      </div>
                                  </div>
                              </form>
                            </div>
                        </div>
                        
                    </div>
                </div>
            </div>
            <div class="inner-wrapper bottom-left"></div>
        </div>
    </a>
</div>
</body>
</html>
点赞