我在一篇joomla文章中使用javascript时遇到了问题。单击submit按钮时,将重新加载整个页面,而不是执行java脚本。当加载到joomla模板的index.php文件中时,代码可以工作,但由于某些原因,从一篇文章加载时就不能工作
<div class="top"><form>
<p style="text-align: right;"><label for="power">Power <input id="power" type="number" name="power" /> mW</label></p>
<p style="text-align: right;"><input class="submit" type="submit" value="Calculate" /></p>
</form></div>
java脚本
<script type="text/javascript">
/*jslint node: true */
/*global $, jQuery, alert*/
"use strict";
var $jQ = jQuery.noConflict();
alert(location.hostname);
jQuery(document).ready(function () {
$jQ("form").submit(function (event) {
alert("Calculate");
event.preventDefault();
});
});
</script>有人能告诉我哪里出了问题吗?
发布于 2018-04-06 18:31:53
如果当您手动将代码添加到PHP文件中而不是放置在文章中时,它可以工作,那么您的编辑器可能正在删除或更改代码。检查你的编辑器设置,比如“允许JS”(这个设置取决于你的编辑器)。或者,尝试将编辑器设置为“none”,看看是否有效。
发布于 2018-04-07 16:37:46
它看起来很好,试着调试ready()调用,在代码中放置一个alert()调用(或者更好的console.log()),并可能将ready事件替换为jQuery的:
<script type="text/javascript">
"use strict";
console.log("script is running");
jQuery(function () {
console.log("hooking into submit");
jQuery("form").submit(function (event) {
event.preventDefault();
console.log("alternate submit");
});
});
</script>然后打开控制台查看日志(key F12),这至少应该会给你一些关于它卡住的地方的指针。
https://stackoverflow.com/questions/49689032
复制相似问题