0

我是 JavaScript 新手,我正在修复别人代码中的错误。我的问题是,每当表单上的用户在文本框中按下回车键时,它都会触发 windows.confirm "messagevar confirm = window.confirm('Remove Design Check Date?')";,我认为正在发生的是当表单加载触发日历时,会捕获一个事件处理程序。

这是原始代码:

$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
});

}
4

1 回答 1

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

函数 removeDesignCheckDate (id){

/// 你的代码;}

于 2013-11-01T22:24:23.317 回答