2011-12-14 61 views
2

我試圖設置我的文本框中的值不是空的時候啓用的複選框組(myinput),否則禁用該組,但它不能按預期方式工作?我究竟做錯了什麼?如何啓用基於文本框值的複選框?

HTML

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 

     function enable_cb() { 
       if ($(this).val() != "") { 
       $("input.group1").removeAttr("disabled"); 
      } 
      else { 
       $("input.group1").attr("disabled", true); 
      } 
     } 

    </script> 
</head> 
<body> 
    <form name="frmChkForm" id="frmChkForm"> 
    <input id="mytext" type="text" onkeydown="enable_cb(this);" /> 

    <input type="checkbox" name="chk9[120]" class="group1"> 
    <input type="checkbox" name="chk9[140]" class="group1"> 
    <input type="checkbox" name="chk9[150]" class="group1"> 
    </form> 
</body> 
</html> 

回答

3

您需要在方法中聲明輸入參數。 'this'不會自動發送到該方法。

function enable_cb(textbox) { 
      if ($(textbox).val() != "") { 
      $("input.group1").removeAttr("disabled"); 
     } 
     else { 
      $("input.group1").attr("disabled", true); 
     } 
    } 
+0

或者更好的是,從jQuery分配事件監聽器,並堅持`$(this)`。 – 2011-12-14 11:49:14

-2

你嘗試過只用$(".group1")$(".group1 input")

+0

`.group1`會給出與當前代碼完全相同的結果。 `.group1 input`不起作用,因爲它會查找一個輸入,該輸入是`.group1`元素的子元素。這與問題完全無關。 – 2011-12-14 11:50:03

相關問題