【问题标题】:Disable input button unless checkbox is checked除非选中复选框,否则禁用输入按钮
【发布时间】:2014-06-02 18:19:01
【问题描述】:

所以我想禁用输入按钮功能,除非选中复选框。我不想要一个特定的复选框,但如果选中一个复选框或多个复选框,则该按钮(即删除按钮)将被启用。我遇到的问题是删除按钮仅在选中第一个复选框时才启用。例如,第二个或第三个复选框被选中,删除按钮仍然被禁用,除非第一个复选框也被选中。

有没有办法解决这个问题?

按钮:

  <input type="submit" id="del_event" name="del_event" value="Delete Event"/>

复选框回显这是编码:

  echo 
   "<tr>
    <td><input type='checkbox' name='check' id='check'/><a href='' class='event-link'>$name</a>  
    </td><td>$date</td><td>$time</td><td>$venue</td>
  </tr>";

我使用的脚本是:

   var chkbox = $("#check"),
            button = $("#del_event");
       button.attr("disabled","disabled");
        chkbox.change(function(){
            if(this.checked){
                button.removeAttr("disabled");
            }else{
                button.attr("disabled","disabled");
            }
        });

感谢任何帮助。

【问题讨论】:

  • #check 作为标识符是您的主要问题 - id 应该是完全唯一的:页面中只有一个元素。

标签: javascript php button checkbox input


【解决方案1】:

像这样更改您的复选框生成的代码:

 echo 
   "<tr>
    <td><input type='checkbox' name='check' class='check'/><a href='' class='event-link'>$name</a>  
    </td><td>$date</td><td>$time</td><td>$venue</td>
  </tr>";

由于您希望为每个复选框触发此事件 您需要在复选框中使用 class 而不是 id 。 在 jquery 中:

       var chkbox = $(".check");
       button = $("#del_event");
       button.attr("disabled","disabled");
       chkbox.change(function(){
            if(this.checked){
                button.removeAttr("disabled");
            }else{
                button.attr("disabled","disabled");
            }
        });

【讨论】:

    猜你喜欢
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2013-04-15
    相关资源
    最近更新 更多