我是JavaScript的新手,我正在修复别人的代码中的错误。我的问题是,每当表单上的用户按下文本框上的回车键时,它就会触发一个windows.confirm“messagevar confirm = window.confirm('删除设计检查日期?')”;我相信发生的事情是表单加载时会捕获一个事件处理程序,触发日历。
以下是原始代码:
$jQuery(document).ready(function(){
$('printableDesignLog').hide();
$('add').hide();
$('orders_Input').hide();
$('newOrder_div').hide();
$('designLog_Details').hide();
$('jobNumber').focus();
getDesigners();
new Ajax.Autocompleter("jobNumber","jobNumbers",
baseURL + "search.php?task=getJobNumbers_WithSeparator",{
minChars: 2,
frequency: 0.1,
indicator: 'indicator',
afterUpdateElement : setJobNumber});
Date.format = 'mmm dd, yyyy';
$j(function(){
$j('#Design_Date')
.datePicker({createButton:true, startDate:'Jan 01, 2006',
endDate:'Dec 31, 2015'})
.bind('click', function(){
$j(this).dpDisplay();
this.blur();
return false;
})
.dpSetOffset(-50, 20) // Move calender just above/right
.bind('dateSelected', function(e, selectedDate, $td){
$('DesignDate').value = selectedDate.format("yyyy-mm-dd");
});
});
$j(function(){
$j('#FollowUp_Dt')
.datePicker({createButton:true, startDate:'Jan 01, 2010', endDate:'Dec 31, 2015'})
.bind('click', function(){
$j(this).dpDisplay();
this.blur();
return false;
})
.dpSetOffset(-150, -150) // move calendar above the button to the left
.bind('dateSelected', function(e, selectedDate, $td){
$('FollowUpDt').value = selectedDate.format("yyyy-mm-dd");
});
});
});
以下是我尝试过的代码。我尝试禁用enter键事件,因为在这种形式下不需要它,我使用javascript原型版本'1.6.0.2'和jquery版本1.2.6但是当我输入以下代码时它不识别keydown,并且它在IE中输出以下错误“无法获取未定义或空引用的属性'keydown'”。谢谢您的帮助。
function preventDefault() {
var e = this.originalEvent;
this.isDefaultPrevented = returnTrue;
if ( !e ) {
return;
}
[// If preventDefault exists, run it on the original event]
if ( e.preventDefault ) {
e.preventDefault();
[// Support: IE]
[// Otherwise set the returnValue property of the original event to false]
} else {
e.returnValue = false;
}
}
jQuery(document).ready(function(){
$('input[type=text]').keydown(function(event){
if (event.which == 13){
$('input[type=text]').unbind();
event.preventDefault();
}
});
});
以下是不断出现的windows.confirm消息框的代码
function removeDesignCheckDate(id)
{
j( ".JQCancelSubmit" ).keypress(function(event) {
var code = (event.keyCode ? event.keyCode : event.which);
if (code == 13) { //Enter
return false;
}
});
var confirm = window.confirm('Remove Design Check Date?') ;
//confirm.blur();
if (!confirm) return ;
var jobNumber = $j('#jobNumber').val() ;
var tmp = id.split('-',2);//split to 0:- field name & 1:- DesignOrderID
//$j('#msg').html(tmp[0] + ' | ' + tmp[1] + ' > '+ $j('#'+id).val());
//i.e. tmp[0] is the field name; tmp[1] is the DesignOrderID
//construct data object:
var d = new Object();
d[eval('"'+tmp[0]+'"')] = $j('#' + id).val();
var data = Object.toJSON(d);
var callback = function(response){
if (response.error) { // display error
$(id).focus();
$j('#msg').html('check your input ...');
setTimeout("$('msg').innerHTML = ''", 3000);//reset the result after 10sec
}else {
$j('#msg').html('Changes Saved ...');
//$j(id).html('') ;
refreshAfterDelete(jobNumber);
setTimeout("$j('#msg').html('')", 3000);//reset the result after 10sec
(tmp[0]).match('Date') ? updateDaysToDesignCompletion(tmp[1]) : '';
}
};
$j.ajax({
type: 'post',
dataType: 'json',
url: controller,
data: 'task=removeDesignCheckDate' + '&DesignOrderID=' + tmp[1] +
'&dummy=' + new Date().getTime(),
success: callback
});
}
答案 0 :(得分:1)
试试这个;
$('body').on('keypress', '.JQCancelSubmit', function (event) {
var code = (event.keyCode ? event.keyCode : event.which);
if (code == 13) { //Enter
return false;
}
});
将css类'JQCancelSubmit'放在您要取消提交的输入上
修改1
or for previus versions this may work:
$('.JQCancelSubmit').bind('keypress',function(event){
var code = (event.keyCode ? event.keyCode : event.which);
if (code == 13) { //Enter
return false;
}
})
修改2
你可以试试这个
$( ".JQCancelSubmit" ).keypress(function(event) {
var code = (event.keyCode ? event.keyCode : event.which);
if (code == 13) { //Enter
return false;
}
});
Edit 3
function removeDesignCheckDate(id){
///你的代码; }