2017-07-27 11 views
1

ドロップダウンボックスと入力ボックスが1つあります。 Aadharがドロップダウンで選択されている場合、私は数値だけを取る入力ボックスのkeydownイベントを割り当てています。 PASSPORTの場合は、英数字の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はパスポートのために選択された後も、それだけで数値入力を取っている場合。このコードはアンドロイドデバイスの返品です。任意のソリューション??おかげさまで あなたが複数のリスナーにあなたがあなたのドロップダウンを実行するたびに取得する他、新しいものを結合する前に、他のKeyDownイベントリスナーを削除する必要が

+0

単一の検証機能を使用することができます。オプションが変更されたときに使用するパターンを変更するだけで済みます。 – inarilo

答えて

0

は、単に要素から属性を削除すると、イベントのバインドを解除しません。

これは完全なコピー/ペーストの回答ではありません。これはあなたのニーズに合わせて調整できる例です。私はちょうどリスナーを割り当ててそれを変更したコードの部分を取ってきました。

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