[英]Drupal 9 - Uncaught ReferenceError: jQuery is not defined
I am trying to use an external Javascript library areYouSure in Drupal 9. I am getting the error "Uncaught ReferenceError: jQuery is not defined".我正在尝试在 Drupal 9 中使用外部 Javascript 库areYouSure 。我收到错误“Uncaught ReferenceError: jQuery is not defined”。
My php code for form looks as below.我的表单 php 代码如下所示。
<?php
function raco_inventory_purchase_form($eid)
{
$out =
'
<script src="/core/assets/vendor/jquery/jquery.min.js"></script>
<script src="/libraries/areyousure/jquery.are-you-sure.js"></script>
<script src="/modules/custom/racoinventory/js/racoinventory.js"></script>
<body>
<form id="myform" autocomplete="off" action="/racoinventory/purchase/entries/' . $eid . '" method="post">
<input type="text" id="yourname" name="yourname" value="' . '" placeholder="Enter your name">
<br><br>
<input type="button" value="Save" onclick="racoinventory_save_data(this);" />
</form>
<script>
jQuery(function()
{
jQuery("#myform").areYouSure(
{
message: "It looks like you have been editing something. "
+ "If you leave before saving, your changes will be lost."
}
);
});
</script>
</body>
';
return $out;
}
?>
My library file looks as below我的库文件如下所示
# Custom module library for general purposes.
racoinventory:
css:
component:
css/racoinventory.css: {}
js:
/libraries/raco/racocommon.js: {}
js/racoinventory.js: {}
/libraries/areyousure/jquery.are-you-sure.js
dependencies:
- core/jquery
- core/jquery.once
And the javascript code looks as below并且 javascript 代码如下所示
function racoinventory_save_data()
{
alert("You hit Save button");
jQuery('#myform').trigger('rescan.areYouSure');
return true;
}
With this code in place, When I load the page , I get below error.使用此代码,当我加载页面时,出现以下错误。
Uncaught TypeError: jQuery(...).areYouSure is not a function
未捕获的类型错误:jQuery(...).areYouSure 不是函数
I am not sure what is going wrong here.我不确定这里出了什么问题。 Can somebody please help me to figure out the issue.
有人可以帮我找出问题所在。
IMO you can try to wrap your code in IMO 您可以尝试将代码包装在
(function ($,jQuery) {
$(document).ready(function(){
Code here
});
})(jQuery);
However, you can check jQuery library version and can load jQuery at bottom.但是,您可以检查 jQuery 库版本并可以在底部加载 jQuery。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.