簡體   English   中英

啟用/禁用用戶輸入確定復選框

[英]Enable/disable checkbox determind by users input

我有一個簡單的表格,用戶輸入他的聯系電話。 如果聯系號碼以07開頭,則啟用該復選框,我需要禁用其他任何內容。

我已經編寫了一些代碼,但我遇到的問題是當用戶鍵入01然后它被禁用但是如果他們繼續在01之后添加任何其他數字則復選框變為啟用狀態。

 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 (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";
  }

將您的if語句更改為:

if(!String(document.getElementById("1").value).startsWith("07")) {...}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM