我的要求非常明確,我不希望字母在我的文本字段中,但不能將文本字段更改爲數字。通過使用以下jQuery我已經實現了,但問題是我不能在文本字段中分配ID。 因爲在我實際的代碼我沒有ID,因此不能使用 $(「#T2」)。在(「的keydown KEYUP」,函數(E)該代碼。 有沒有其他的替代調用。在不知道文本字段ID的情況下調用jquery函數
繼代碼工作,但不能在現實
<!doctype html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(function() {
var regExp = /[a-z]/i;
$('#t2').on('keydown keyup', function(e) {
var value = String.fromCharCode(e.which) || e.key;
// No letters
if (regExp.test(value)) {
e.preventDefault();
alert("only nos allowed");
return false;
}
});
});
</script>
<body>
<form>
<input type="text" name="t1" id="t2">
</form>
</body>
</html>
'的問題是,我不能在文本field'分配ID ...但你的文本字段中確實有一個ID,不是嗎? 'id =「t2」' –
$(「input」)。on ... –
您的輸入有一個ID ..無論類型。類型不支配元素屬性 – ThisGuyHasTwoThumbs