2017-07-27 45 views
1

有一个下拉菜单和输入框。如果在下拉列表中选择Aadhar,我将为只有数字的输入框分配keydown事件。在PASSPORT的情况下,我正在更换采用字母数字字符的keydown事件。根据输入keydown的下拉更改应该完成某些功能

function changeDocumentType(pthis) { 
 
    //dropdown change 
 
\t var htmlid = "#" + pthis.id; 
 
     var doctype = $(htmlid+':selected').val(); 
 
     populateValidations(doctype,"docId"); 
 
    } 
 

 
    function populateValidations(doctype,inputId){ 
 
\t  $("#"+inputId).removeAttr("onkeydown"); 
 
     $("#"+inputId).removeClass("onlyNumeric"); 
 
     $("#"+inputId).removeClass("alphabetNumeric"); 
 
     if(doctype === "1"){ 
 
     //aadhar 
 
      $("#"+inputId).addClass("onlyNumeric"); 
 
      $("#"+inputId).attr("onkeydown", "return inputOnlyNumberTablet(this,event,12)"); 
 
     } 
 
     if(doctype === "2") { 
 
     //passport 
 
       $("#"+inputId).addClass("alphabetNumeric"); 
 
       $("#"+inputId).attr("onkeydown", "return inputOnlyAlphaNumericTablet(this,event,10)"); 
 
     } 
 
    } 
 

 
    function inputOnlyAlphaNumericTablet(pthis, event, length){ 
 
\t $('.alphabetNumeric').on('input', function(event) { 
 
      if (pthis.value.length <= parseInt(length, 10)) { 
 
       pthis.value = pthis.value.replace(/[^a-z0-9]/gi, ''); 
 
      } else { 
 
       pthis.value = pthis.value.substr(0, pthis.value.length - 1); 
 
      } 
 
     }); 
 
    } 
 
    } 
 

 
    function inputOnlyNumberTablet(pthis, event, length){ 
 
\t $('.onlyNumeric').on('input', function(event) { 
 
      if (pthis.value.length <= parseInt(length, 10)) { 
 
       pthis.value = pthis.value.replace(/[^0-9]/g, ''); 
 
      } else { 
 
       pthis.value = pthis.value.substr(0, pthis.value.length - 1); 
 
      } 
 
     }); 
 
    } 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input id="docId" type="text" value="" enabled="enabled" > 
 
    <select style="" value="" enabled="enabled" onchange="changeDocumentType(this)" id="DocumentListFile"> 
 
    <option value="">Select</option> 
 
    <option value="1">Aadhar ID</option> 
 
    <option value="2">PassPort</option> 
 
    </select>

如果一旦aadhar选择了护照也正在采取只输入数字。此代码是Android设备的返回。任何解决方案 谢谢。

+0

您可以使用单个验证功能,您只需更改选项更改时使用的模式。 – inarilo

回答

0

你必须结合新的人之前,先删除其他的keydown听众否则你得到多个听众每次执行你的下拉时间,

只需去除元素属性不取消绑定事件。

这不是一个完整的复制/粘贴答案。这是一个例子,您可以根据自己的需求进行调整。我刚刚拿走了代码中指定侦听器并更改它的那部分代码。

function populateValidations(doctype,inputId){ 
    var inp = $("#"+inputId); // get this once. Easier if there is a name change etc 
    inp.unbind('keydown'); // unbind any keydown event that may exist 
    inp.removeClass("onlyNumeric"); 
    inp.removeClass("alphabetNumeric"); 
    if(doctype === "1"){ 
     //aadhar 
     inp.addClass("onlyNumeric"); 
     // add your keydown 
     inp.keydown(function(){ return inputOnlyNumberTablet(this,event,12);}); 
    } 

    if(doctype === "2") { 
     //passport 
     inp.addClass("alphabetNumeric"); 
     inp.keydown(function(){return inputOnlyAlphaNumericTablet(this,event,10);}); 
    } 
} 
+0

我试图解绑,但它不工作。 – user8375429