启用/禁用用户输入确定复选框 [英] Enable/disable checkbox determind by users input

查看:80
本文介绍了启用/禁用用户输入确定复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表格,用户输入他的联系电话。如果联系电话号码以07开头,则启用该复选框,我需要将其禁用。

I've got a simple form where the user enters his contact number. If the contact number starts with 07 then the checkbox is enabled anything else I need it to be disabled.

我写了一些代码,但我遇到的问题是当用户类型01然后它被禁用,但如果他们继续在01之后添加任何其他数字,则复选框变为启用。

I have written some code but the problem I face is when the user types 01 then it's disabled but if they then go on to add any other numbers after the 01 the checkbox becomes enabled.

function deselectcheckbox(wildvalue) {
  if (document.getElementById("1").value == "01") {
   $("#checkbox").attr("disabled", true);
   document.getElementById("divText").innerHTML = "Not able to send";
  }
  else
   {
   $("#checkbox").attr("disabled", false);
   document.getElementById("divText").innerHTML = "Send text";
  }
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
<tr>
 <td>Contact Number</td>
 <td><input type="text" id="1" name="1" onkeyup="deselectcheckbox(this.value)"></td>
</tr>
<tr>
 <td><label><div id="divText">Send text</div></label></td>
 <td><input type="checkbox" name="contact" id="checkbox" value=""></td>
</tr>
</table>

我试过添加一个通配符

(document.getElementById("1").value == "01*")

但它不起作用,请帮助

推荐答案

如果您只想启用该复选框当内容以07开头时,您可以先检查该内容,然后在else块内禁用。

If you only want the checkbox to be enabled when the content starts with "07" you can check for that first and disable inside of the else block.

  if (document.getElementById("1").value.startsWith("07")) {
    $("#checkbox").attr("disabled", false);
    document.getElementById("divText").innerHTML = "Send text";
  }
  else
  {
    $("#checkbox").attr("disabled", true);
    document.getElementById("divText").innerHTML = "Not able to send";
  }

这篇关于启用/禁用用户输入确定复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆