如何根据下拉选项更改键盘值长度?

时间:2016-06-01 11:28:00

标签: javascript jquery

这是基于每一行,因此我将其加载到文档中,如下所示:

$(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 characters等等。

如果他们输入的数字超过或者不足,我只需要提醒一下。 我试图像上面那样做一个很长的功能,但我认为它根本不会起作用。

1 个答案:

答案 0 :(得分:1)

如评论中所述。要分配值,您应该在定义变量时使用=而不是==

以下内容适用于您的一个长度。您应该根据需要编辑代码以使其适用于多种长度。我在jQuery $选择器周围添加了大括号。

例如:$ 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>