[英]my js code gives me an error saying that “property is not defined” eventhough it exists in my object
[英]Can some tell me where is the error? saying $ 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>
您是否忘了在代碼之前在標記中包含jquery.js文件?
設置腳本引用的一種普遍接受的方法如下(還有其他方法,這不是萬能的):
<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
屬性。
1.在head
部分的first script tag
中是否包含了Jquery
lib。 在這種情況下,將僅執行基於jquery的代碼。
2.是否wrap the code into $(document).ready()
函數中。
3您是否正在使用PHP
。 因此需要將$
替換$
Jquery.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.