【问题标题】:Check checkbox when textbox have specific number of characters当文本框具有特定数量的字符时选中复选框
【发布时间】:2016-01-12 21:37:59
【问题描述】:

您好,当用户在我的输入文本框中输入 8 位数字时,我试图让我的复选框被选中。现在我可以在用户输入时选中复选框,但我是新用户,无法弄清楚。感谢您的帮助。

<input id="thecheckbox" type="checkbox">
<input id="theinput" type="text">

    $("#theinput").keyup(function () {
    if ($(this).val() == "") {
        $("#thecheckbox").prop("checked", false);
    } else {
        $("#thecheckbox").prop("checked", true);
    }
});

$("#thecheckbox").change(function () {
    if (!$(this).is(':checked')) {
        $("#theinput").val("");
    }
});

【问题讨论】:

    标签: javascript html checkbox input


    【解决方案1】:

    尝试使用$(this).val().length != 8,而不是$(this).val() == ""

    【讨论】:

    • @JosephMarikle 是的,我忘记在粘贴后添加该死的.length。固定。
    【解决方案2】:

    你的意思是这样的吗?

    $("#theinput").on('input', function () {
      if (this.value.length >= 8) {
        $("#thecheckbox").prop("checked", true);
      } else {
        $("#thecheckbox").prop("checked", false);
      }
    });
    
    /* $("#thecheckbox").change(function () {
      if (!$(this).is(':checked')) {
        $("#theinput").val("");
      }
    }); */
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input id="thecheckbox" type="checkbox">
    <input id="theinput" type="text">

    【讨论】:

    • 这个工作在这里,但我添加到我的代码并且不工作,我不知道这是我的表单的问题。我的完整表单代码。
    • @JahlilEspinoza 我确实注释掉了.change() 代码。我认为那会破坏它。否则,如果您可以在jsfiddle.net 上复制您的代码,我会看看它。
    • 我复制了我的代码,在这里你可以看到它 jsfiddle.net/hj9rss5g
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    相关资源
    最近更新 更多