虽然我经常在stackoverflow上浏览答案,但这是我在这里发布的第一篇帖子。我遇到的问题是IE8会不停地抛出"Object Expected"错误。我使用了IE8的开发工具,它指向了“mymh.js”文件。
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/MyMHome/javascript/mymh.js"></script>
mymh.js文件只有以下代码
$(document).ready(function() {
$('#hNumber').focus();
$('#ddlDir').change(function () {
var selVal = $('#ddlDir').val();
if (selVal == 'N' || selVal == 'S' || selVal == 'E' || selVal == 'W'){
$.getJSON('/MyMHome/DimeServlet?strDir='+$('#ddlDir option:selected').val(), function(data) {
$('#ddlSt')
.find('option')
.remove()
.end()
$.each(data, function(i,field){
var name = field;
$('#ddlSt')
.append('<option value= ' + '"' + name + '"' + '>' + name + '</option>');
});
});
$('#ddlSt').focus();
}else{
$('#ddlSt')
.find('option')
.remove()
.end()
.append('<OPTION selected value="">Choose a direction first</OPTION>');
}
})
.trigger('change');
$('#reset').click(function(){
$('#ddlSt')
.find('option')
.remove()
.end()
.append('<OPTION selected value="">Choose a direction first</OPTION>');
$('#hNumber').focus();
});
$('#hNumber').bind('keyup', function() {
if($('#hNumber').val().length == 5){
$('#ddlDir').focus();
}
});
$('#submitQuery').click(function(){
var houseNumber = $('#hNumber').val();
if(houseNumber.replace(/\s+/g, '').length == 0){
alert('Please enter a house number.');
$('#hNumber').focus();
return false;
}else if( (!$.isNumeric(houseNumber)) || houseNumber.indexOf('-') > -1 || houseNumber.indexOf('.') > -1){
alert('Please enter numbers only. You will be prompted later, if the address requires a suffix.');
$('#hNumber').focus();
return false;
}else if(houseNumber < 100 || houseNumber > 12999){
alert('Please enter a house number between 100 and 12999');
$('#hNumber').focus();
return false;
}else if($('#ddlDir option:selected').val() == 'none'){
alert('Please select a street direction.');
$('#ddlDir').focus();
return false;
}
});
$('form').keypress(function(e) {
if (e.keyCode == '13') {
e.preventDefault();
if($('#ddlSt').is(":focus")){
$('#submitQuery').trigger('click');
}
else{
return false;
}
}
});
});
错误指向
<script ... mymh.js></script>
,但在调试器中它指向$document.ready(function() {
。有人看到IE8为什么会不断抛出该错误吗?
$(document.documentElement).ready(function(){
。 - Danilo ValentejQuery
替换掉第一个$
吗?例如:jQuery(document).ready(function($) { /* code */ });
- rjz