2017-12-02 10 views
0

javascript関数を使用して値を追加しています.... onkeypressイベントを使用すると、スラッシュ(/)が与えられたときに値が警告メッセージとともに正しく追加されます。 ..but iの値が配列に正しく追加取得されていないonpasteイベントを使用する場合....誰かがこのonpasteイベントが正しく機能していない

function onlyAlphabets(e, t) { 
 
    
 
\t try { 
 
     if (window.event) { 
 
      var charCode = window.event.keyCode; 
 
      
 
     } 
 
     else if (e) { 
 
      var charCode = e.which; 
 
      
 
     } 
 
     else { return true; } 
 
     
 
     if ((charCode == 47)){ 
 
     \t 
 
     \t alert("Please use any other charachter! "); 
 
     return false; 
 
     } 
 
     else{ 
 
      return true; 
 
     } 
 
    } 
 
    catch (err) { 
 
     alert(err.Description); 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div class="table-responsive"> 
 
<table class="table table-striped table-bordered table-fixed table-hover table-condensed" id='+tables+' onkeypress="return onlyAlphabets()" > 
 
<thead> 
 
<tr><th></th><th>Code</th><th>Sub Category</th></tr> 
 
</thead> 
 
<tbody class="tbody"></tbody> 
 
</table> 
 
</div>';

+0

あなたが持っている場合は... else if ... else'後で何も実行されません... – alfasin

+0

あなたはどこでペーストイベントを聞きますか?ペーストイベントでやっている?何が動作しません。 – Kaiido

+0

私はonpasteを使用している場合keypressイベントを使用していない.... that doesnt work – usr

答えて

0

で私を助けることができる。このような<body>を追加し、再試行してください。

function onlyAlphabets(e, t) { 
 
    
 
\t try { 
 
     if (window.event) { 
 
      var charCode = window.event.keyCode; 
 
      
 
     } 
 
     else if (e) { 
 
      var charCode = e.which; 
 
      
 
     } 
 
     else { return true; } 
 
     
 
     if ((charCode == 47)){ 
 
     \t 
 
     \t alert("Please use any other charachter! "); 
 
     return false; 
 
     } 
 
     else{ 
 
      return true; 
 
     } 
 
    } 
 
    catch (err) { 
 
     alert(err.Description); 
 
    } 
 
}
<body onkeypress="return onlyAlphabets()"> 
 
    <div class="table-responsive"> 
 
     <table class="table table-striped table-bordered table-fixed table-hover table-condensed" id='+tables+' > 
 
      <thead> 
 
       <tr><th></th><th>Code</th><th>Sub Category</th></tr> 
 
      </thead> 
 
      <tbody class="tbody"></tbody> 
 
     </table> 
 
    </div> 
 
</body>

あなたが<input>のようではない要素にキープレスイベントを添付する場合は、キーを押したとき、フォーカスはその要素ではありません。
これは、テーブルがイベントを認識できなかった理由です。

+0

keypressイベントのためにはうまくいきました......しかし、私はonpasteイベントを提供する必要があります.....この(onpaste)イベントのためには動作しません。 – usr

+0

あなたが与えたコードは、 keypressで作業するので、最初に修正しました... – moon

関連する問題