说$ not defined,object expected ..实际上我想验证单击按钮时是否所有单选按钮都检查!帮助PLZ
<script type="text/javascript">
$(document).on('click', 'form', function () {
var validate = true;
var unanswered = new Array();
// Loop through available sets
$('.qselections').each(function () {
// Question text
var question = $(this).prev().text();
// Validate
if (!$(this).find('input').is(':checked')) {
// Didn't validate ... dispaly alert or do something
unanswered.push(question);
validate = false;
}
});
if (unanswered.length > 0) {
msg = "Please answer the following questions:\n" + unanswered.join('\n');
alert(msg);
}
return validate;
});
</script>
答案 0 :(得分:0)
您是否忘记在代码之前在标记中包含jquery.js文件?
答案 1 :(得分:0)
一种普遍接受的设置脚本引用的方法如下(还有其他方法,这不是全部的全部):
<html>
<head>
</head>
<body>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="MyScriptFile.js"></script>
<script>
$(function() {
$(document).on('click', 'form', function () {
var validate = true;
var unanswered = new Array();
// Loop through available sets
$('.qselections').each(function () {
// Question text
var question = $(this).prev().text();
// Validate
if (!$(this).find('input').is(':checked')) {
// Didn't validate ... dispaly alert or do something
unanswered.push(question);
validate = false;
}
});
if (unanswered.length > 0) {
msg = "Please answer the following questions:\n" + unanswered.join('\n');
alert(msg);
}
return validate;
});
</script>
</body>
</html>
通过这种方式,您可以确保(1)在任何脚本尝试执行脚本之前加载页面;(2)在其他可能要使用它的脚本之前包含jQuery。
这不是万无一失的,您可能(最终)需要使用Require.js之类的内容或更简单的defer
属性。
答案 2 :(得分:0)
1.您是否在Jquery
部分的first script tag
中添加了head
个库。在这种情况下,只会执行基于jquery的代码。
2.Did you wrap the code into $(document).ready()
function。
3你在角落里使用PHP
吗?因此需要将$
替换为Jquery.