我一直在嘗試創建一個提交按鈕,當用戶在輸入框中鍵入時自動啟用/禁用。我已經成功地創建了一個提交按鈕,該按鈕從禁用開始,然后在用戶開始輸入時啟用,但我希望在用戶輸入數字或除字符或字串之外的任何其他內容時禁用該按鈕。
這是我目前的腳本代碼:
$(document).ready(function() {
$('#seed').on('keyup', function(){
var regEx = /^[a-zA-Z\s]*$/;
if($(this).val() != regEx) {
$('#submit').prop('disabled', false);
} else {
$('#submit').prop('disabled', true);
}
});
});
連同我的主索引檔案和我的表單:
<head>
<title>Story Ideas</title>
<link type="text/css" rel="stylesheet" href="style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script defer src="js/script.js"></script>
</head>
<div class="form-style">
<form class="form" name="form" method="post" action="?v=ture">
<label for="table">Select Your Story</label> <br />
<select name="table" id="table">
<option value="moons">Moons</option>
<option value="lionbird">Lionbird</option>
<option value="chik">Chik</option>
</select>
<br>
<input type="text" id="seed" name="seed">
<br>
<input id="submit" type="submit" disabled="disabled">
</form>
</div>
<?php
include_once('includes/functions.php');
if (isset($_GET['v']) && $_GET['v'] == true)
{
$t = $_POST['seed'];
$table = $_POST['table'];
echo genPoem($t, $table);
echo "<br>";
echo "<br>";
}
?>
</body>
我嘗試使用 var regEx = /^[a-zA-Z\s]*$/; 以防止這種情況。我錯過了什么嗎?
uj5u.com熱心網友回復:
您沒有使用正確的條件來檢查您的值是否與正則運算式匹配,您的正則運算式很好,只需切換if($(this).val() != regEx)為if(regEx.test($(this).val())).
test方法參考:https :
//developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
uj5u.com熱心網友回復:
您可以嘗試$(this).val().match(regEx)代替使用!=,如https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 中所述。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/368957.html
標籤:javascript html 查询 形式 按钮
