2011-02-17 77 views
4

我想阻止我的用户在数字字段中输入字母。 我看到有一个选项:editrules:{number:true},但是这个选项会让用户点击用户想要的任何按键,只有当保存行时它会提醒非法输入。这对我来说不是好选择。我想从一开始就防止键入不是数字的键入(例如,在常规输入中,我可以使用jQuery的.numeric())。jqGrid只允许在编辑单元格时编号

这怎么办?

回答

3

我不使用jQuery.numeric插件自己,但我想你应该使用的editoptionsdataInit属性对应的网格列:

editoptions: { dataInit: function (elem) { 
        $(elem).numeric(/*some optional parameters*/); 
       } 
} 

或形式有些麻烦的情况下

editoptions: { dataInit: function (elem) { 
        setTimeout(function(){ 
         $(elem).numeric(); 
        }, 100); 
       } 
} 

我希望它能工作。

+1

伟大的答案!如果你想只允许整数,你可以使用'$(elem).numeric(false,function(){alert(“Integers only”); this.value =“”; this.focus();});`。见[这里](https://github.com/SamWM/jQuery-Plugins/blob/master/numeric/test.html)。 – Filly 2014-01-03 11:09:45

4
 {name:'actualNo',index:'actualNo',editable:true, edittype:"text", width:150,editoptions:{ 
           size: 15, maxlengh: 10, 
           dataInit: function(element) { 
            $(element).keyup(function(){ 
             var val1 = element.value; 
             var num = new Number(val1); 
             if(isNaN(num)) 
             {alert("Please enter a valid number");} 
            }) 
           } 
          }}, 
1

{名称: '速度',指数: '速度',对齐: “左”,宽度: '150',可编辑:真正的,

  edittype:"text", editoptions:{ 
       size: 25, maxlengh: 30, 
       dataInit: function(element) { 
        $(element).keypress(function(e){ 
         if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) { 
          return false; 
         } 
        }); 
       } 
      } 
     }, 
相关问题