gpt4 book ai didi

除了选择文档之外,JQuery 选择器似乎不起作用

转载 作者:行者123 更新时间:2023-12-01 06:28:18 24 4
gpt4 key购买 nike

我一直在尝试学习如何将 jQuery AJAX 与 PHP 结合使用。

我的问题是,每当我选择除 document 之外的任何其他元素时,似乎都不起作用。我想要的只是在按下提交按钮无济于事后阻止页面加载。

我尝试使用 jQuery 而不是 $,但仍然没有成功。

老实说,我不确定自己在做什么,并且我尝试了几个小时来寻找此问题的解决方案。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
$("p").click(function() {
alert('test');
});
$("form").submit(function() {
alert('test');
return false;
});
</script>
<title>Insert title here</title>
</head>
<body>
<form action="process.php" method="post">
Inputs:<br />
<textarea rows="15" cols="60" id="input" name="input">Some text...
</textarea><br /><br />
Start:
<input type="text" id="start" name="start" />
End:
<input type="text" id="end" name="end" /><br />
<input type="submit" id="submit" name="submit" value="Submit">
</form>
<p id="output">Output: Some Random Text</p>
</body>
</html>

最佳答案

您的 JavaScript 在页面其余部分加载之前运行。要修复此问题,请将脚本移至页面底部(在结束 </body> 标记之前),或使用 jQuery .ready() method 将事件处理程序绑定(bind)到 DOM 就绪事件。 :

$(document).ready(function () {
// Your code in here
});

//or, use the shorthand form:

$(function () {
// Your code here
});
<小时/>

旁注

I've tried using jQuery instead of $, and still no luck.

jQuery之间没有区别和$ 。两者都只是保存对同一对象的引用的标识符。通常使用 $ 更容易(只是因为它更短)除非您使用除 jQuery 之外的另一个库也需要该标识符(例如 PrototypeJS),在这种情况下您可以告诉 jQuery 放弃对 $ 的控制标识符为 .noConflict()方法。

关于除了选择文档之外,JQuery 选择器似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12767235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com