[英]Code snippet works but not in my html file
So basically, I've been looking at different questions and found a code snippet that worked perfectly in the snippet run.所以基本上,我一直在研究不同的问题,并找到了一个在代码片段运行中完美运行的代码片段。 But when I copied it into my HTML file, it doesn't work.
但是当我将它复制到我的 HTML 文件中时,它不起作用。
My apologies if I sound like an idiot I'm a bit of a biggener.如果我听起来像个白痴,我很抱歉,我有点大。
I've checked other questions including jQuery snippet doesn't work and Code works in Codepen, but not with my desktop files我检查了其他问题,包括jQuery 片段不起作用和代码在 Codepen 中工作,但不适用于我的桌面文件
But still couldn't seem to find a solution.但似乎仍然找不到解决方案。
My code is as follows.我的代码如下。
<script> $(document).ready(function() { $('#newsletter').on('click', '#newsletterButtonSubmit', function() { var inputVal = $(this).parent('div').find('#newsletterInputEmail').val(); console.log(inputVal); }); }); </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="newsletter"> <div class="newsletter"> <h6 class="headerDIV">SUBSCRIBE TO OUR NEWSLETTER</h6> <input id="newsletterInputEmail" class="input-email" type="email" placeholder="Enter your email here"> <button id="newsletterButtonSubmit" width="100px" class="w3-button w3-round-xlarge w3-grey">Submit</button> <p><label id="newsletterEmailMSG" class="textFillField">This field is mandatory</label></p> <p><input id="newsletterCheckbox" class="w3-check" type="checkbox"><label class="textRights">I have read and accepted the general terms and conditions*</label></p> <p><label id="newsletterCheckboxMSG" class="textFillField">This field is mandatory</label></p> <label class="textRights2">See our <a href="politics/privacy/politica_de_privacidade_pt.pdf" target="_blank" style="text-decoration: underline;">Privacy Notice</a> for more information.</label> </div> </div>
When I open the file in my browser, the script part doesn't work at all.当我在浏览器中打开文件时,脚本部分根本不起作用。 It does nothing.
它什么也不做。
Any help is greatly appreciated:!任何帮助是极大的赞赏:! :)
:)
Next to the order of loading, you have forgot the thing that the variable $ is not defined在加载顺序旁边,您忘记了变量 $ 未定义的事情
var $ = jQuery.noConflict();
Full example:完整示例:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
var $ = jQuery.noConflict();
$(document).ready(function() {
$('#newsletter').on('click', '#newsletterButtonSubmit', function() {
var inputVal = $(this).parent('div').find('#newsletterInputEmail').val();
console.log(inputVal);
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.