2017-02-01 164 views
1

我已經創建了一個輸入框,通過使用tab鍵進行驗證,但是需要在按下回車鍵後進行驗證。如何通過輸入密鑰驗證答案?

enter image description here

這裏,刻度顯示符號後,才輸入正確的答案,然後按Tab鍵。然後它轉到下一個輸入框,並進行驗證。

但是,當我輸入正確的答案並敲入輸入框本身內部的輸入時,驗證需要發生。

對於我試過這個JS代碼:

$(document).keypress(function(e) { 
    if(e.which == 13) { 
    checktxt(h,v,w,c) 
    } 
}); 

function checktxt(h,v,w,c) { 
    var th=$("#"+h).val(); 
    var tv=$("#"+v).val(); 
    if(th.toLowerCase()==tv.toLowerCase()) { 
    $("."+c).show(); 
    $("."+w).hide(); 
    } else if(tv.toLowerCase()=="") { 
    } else { 
    $("."+c).hide(); 
    $("."+w).show(); 
    } 
} 

而且,當我按下回車鍵沒有得到驗證。

而我的HTML代碼

<div> 
    <input id="texthidden1" type="hidden" value="877" /> 
    <input id="textvisible1" type="text" value="" onblur="javascript:checktxt('texthidden1','textvisible1','wrong1','correct1');" /> 
    <div class="wrong1"><img src="../images/smallwrong.png"/></div> 
    <div class="correct1"><img src="../images/smallgreen.png"/></div> 
</div> 

enter image description here

我還需要消失蜱圖像當我刪除輸入框中的內容。

回答

2

與你代碼的問題是這部分在按鍵聽者

checktxt(h,v,w,c) 

H,V,W和C是不確定的位置,因爲對的onblur工作正常事件的代碼,你想複製相同在輸入新聞時,您需要在您的按鍵事件中定義這些變量。

如果你檢查你的html,你傳遞所有元素的ID到你的JS中的checktxt函數。

在keypress偵聽器中也做同樣的事情。

$(document).keypress(function(e) { 
    if(e.which == 13) { 
    checktxt('texthidden1','textvisible1','wrong1','correct1') 
    } 
}); 

$(document).keypress(function(e) { 
 
    if (e.which == 13) { 
 
    checktxt('texthidden1', 'textvisible1', 'wrong1', 'correct1') 
 
    } 
 
}); 
 

 
function checktxt(h, v, w, c) { 
 
    var th = $("#" + h).val(); 
 
    var tv = $("#" + v).val(); 
 
    if (th.toLowerCase() == tv.toLowerCase()) { 
 
    $("." + c).show(); 
 
    $("." + w).hide(); 
 
    } else if (tv.toLowerCase() == "") {} else { 
 
    $("." + c).hide(); 
 
    $("." + w).show(); 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div> 
 
    <input id="texthidden1" type="hidden" value="877" /> 
 
    <input id="textvisible1" type="text" value="" onblur="javascript:checktxt('texthidden1','textvisible1','wrong1','correct1');" /> 
 
    <div class="wrong1"> 
 
    <img src="../images/smallwrong.png" /> 
 
    </div> 
 
    <div class="correct1"> 
 
    <img src="../images/smallgreen.png" /> 
 
    </div> 
 
</div>

+0

非常感謝。它的工作的罰款。 – Anitha

+0

當我刪除輸入框的內容時,我還需要勾選圖像消失。 – Anitha

+0

@Anitha你可以在輸入上綁定一個更改事件偵聽器,並檢查該值是否爲空。 – Deep