这是基于每一行的,所以我将其加载到文档中,如下所示:
$(document).on('keyup','.myNumber',function(){
var $this = $(this);
var lengthA == 16;
var lengthB == 8;
var lengthC == 8;
var lengthD == 10;
var lengthE == 15;
var lengthG == 15;
var lengthH == 10;
var $row = $this.closest("tr").find('.dropDownSelect');
if ($row.val() == "Credit Card") { // use $this.val()
if ($this.val().length == lengthA){
return true;
} else {
return false;
} else if ($row.val){}
} else
$row.removeAttr("disabled");
});
HTML:
<html>
<table>
<tr>
<td>
<select>
<option value="1">1</option> //If this is selected, then #myNumber must have 16 digits
<option value="2">2</option> //If this is selected, then #myNumber must have 8 digits
<option value="3">3</option> //If this is selected, then #myNumber must have 8 digits
<option value="4">4</option> //If this is selected, then #myNumber must have 10 digits
<option value="5">5</option> //If this is selected, then #myNumber must have 15 digits
<option value="6">6</option> //If this is selected, then #myNumber must have 15 digits
<option value="7">7</option> //If this is selected, then #myNumber must have 10 digits
</select>
</td>
<td>
<input type="text" id="myNumber"/> //onkeyup of this the function will run and find out how much integers it has
</td>
</tr>
</table>
</html>
如果下拉值为 lengthA,则 myNumber
需要为 16 个字符。 LengthB = 8 个字符
依此类推。
如果他们输入的数字超过或低于,我只需要一个警报说明这一点。 我正在尝试创建一个像上面这样的长函数,但我认为它根本不会起作用。
最佳答案
正如评论中提到的。要分配值,您在定义变量时应使用 =
而不是 ==
。
以下内容适用于您的长度之一。您应该编辑代码以使其根据需要适用于多种长度。我在 jQuery $
选择器周围添加了大括号。
例如:$row 变为 $(row)。
$(document).on('keyup','#myNumber',function(){
var input = $(this);
var lengthA = 16; // <-- Not == Should be =
var lengthB = 8;
var lengthC = 8;
var lengthD = 10;
var lengthE = 15;
var lengthG = 15;
var lengthH = 10;
var row = $(this).closest("tr").find('.dropDownSelect');
if ($(row).val() == "1") { // use $this.val()
if ($(input).val().length == lengthA){
alert('true');
return true;
} else {
return false;
}
} else
$(row).removeAttr("disabled");
});
HTML
<html>
<table>
<tr>
<td>
<select class="dropDownSelect">
<option value="1">1</option> //If this is selected, then #myNumber must have 16 digits
<option value="2">2</option> //If this is selected, then #myNumber must have 8 digits
<option value="3">3</option> //If this is selected, then #myNumber must have 8 digits
<option value="4">4</option> //If this is selected, then #myNumber must have 10 digits
<option value="5">5</option> //If this is selected, then #myNumber must have 15 digits
<option value="6">6</option> //If this is selected, then #myNumber must have 15 digits
<option value="7">7</option> //If this is selected, then #myNumber must have 10 digits
</select>
</td>
<td>
<input type="text" id="myNumber"/> //onkeyup of this the function will run and find out how much integers it has
</td>
关于javascript - 如何根据下拉选项更改 keyup 值长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37567360/