jQuery,在运行jQuery语句时出现“500内部服务器错误”
jQuery, getting "500 Internal Server Error" when running a jQuery statement
每当触发jQuery时,我都会收到错误500 Internal Server Error
,有没有人知道为什么下面的代码可能导致这种情况?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="../meta/js/jquery-1.7.2.js"></script>
<script>
$(document).ready(function() {
$('#form1').submit(function(e){
e.preventDefault();
alert('I clicked it');
});
});
</script>
</head>
<body>
<form name="form1" method="post">
<button id="button">grab user data</button>
<select></select>
</form>
</body>
</html>
我要猜测这是因为这个变量:
<%=button.ClientID %>
没有其他任何东西(显示)可以表示任何其他会引发错误的东西。
你能澄清一下"jQuery被触发"是什么意思吗?这是否意味着在"点击"时?或者只是通过加载此页面(我假设后者)
我猜它给出了以下行的错误:
$('#<%=button.ClientID %>')
改为对值进行硬编码并对其进行测试:
$('#buttonID')
为了实现你想要的,这将完成这项工作:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#myButton').on('click',function(){
alert("i clicked it");
});
});
</script>
</head>
<body>
<button id="myButton">Click Me!</button>
</body>
</html>
$(document).ready
用于在页面呈现后立即执行代码,然后$('myButton')
将获得任何 id 为"myButton"的元素,然后您将使用方法 "on" 附加您想要的任何事件,在这种情况下,您将需要选择"click"事件,然后您应该提供一个匿名函数,您将在其中将要执行的代码放在该操作上。
您的按钮位于带有 action=" 的标签内。这将导致回发到"/"。如果您的服务器无法正确处理此问题,您将收到内部服务器错误。
如果只想显示警报,则可以在 jQuery 事件对象上调用 preventDefault。Se 示例如下:
$(document).ready(function() {
$('#<%=button.ClientID %>').click( function(e) {
e.preventDefault();
alert("i clicked it");
});
});
编辑:忘记我上面写的。它不起作用。请改用表单上的提交事件。喜欢这个:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="@{'/public/javascripts/jquery-1.6.4.min.js'}" type="text/javascript" charset="${_response_encoding}"></script>
<script>
$(document).ready(function() {
$('#form1').submit(function(e){
e.preventDefault();
alert('I clicked it');
});
});
</script>
</head>
<body>
<form id="form1" method="post" action="">
<button id="button">grab user data</button>
<select></select>
</form>
</body>
</html>
您将submit
事件附加到#form1
但表单没有 id,只有名称。您必须按名称、$("form[name='form1']")
选择表单或为其指定 ID id="form1"
。
相关文章:
- 角度ng-repeat不更新,当使用jQuery内部指令调用Angular 方法时
- 如果它在 jQuery 内部,则重复将不起作用
- 如何访问jquery内部数据
- jquery内部的注释更多的是jquery
- 如何将变量传递给JQuery内部的函数
- Jquery内部引号添加了一个变量
- 调用jquery内部定义的javascript方法
- 从jquery内部的另一个javascript调用函数
- jQuery内部服务器错误
- jquery内部用户控件动态创建
- jquery内部的Javascript抛出语句准备好了
- 访问javascript native "this"jQuery内部
- jQuery内部的jQuery函数都没有按预期工作
- 使用jquery内部或外部文档准备
- 谷歌饼图使用Jquery内部的一个函数
- jQuery内部html主体
- jQuery内部函数不工作
- Jquery内部ajax (get)不能在phonegap + android 2.2上工作
- PHP Ajax加载文件与jQuery内部文件
- jquery内部OOP函数的问题