【问题标题】:Disable checkbox if empty record如果记录为空则禁用复选框
【发布时间】:2014-03-18 05:33:42
【问题描述】:

如果列为空,我这里有禁用复选框。我有pr 列,如果该列为空,复选框被禁用。我也有下拉菜单,如果我选择它会隐藏另一个链接。

但真正的问题是当我选择添加按钮时,应该只允许检查空列,否则有值的列将被禁用。如果我选择删除/编辑所有具有值的列,则只应检查。

请帮忙?

代码:

<script type="text/javascript" language="javascript">  
function setCheckboxes3(act) {
    $('.checkbox').not(':disabled').prop('checked', act == 1 ? true : false);
}
</script>
<script>
jQuery(function ($) {
    $('a.button.edit, a.button.remove, a.button.add').click(function () {
        if ($('input[name="checkbox[]"]:checked').length == 0) {
            return;
        }

        if ($(this).hasClass('edit')) {
            if (!confirm('Edit this Record(s)?')) {
                return
            }
        } else /*if ($(this).hasClass('remove')) */{
            if (!confirm('WARNING !!! All Purchase Order that included in this Record(s) will be deleted.')) {
                return
            }
        }

        var frm = document.myform;
        if ($(this).hasClass('edit')) {
            frm.action = 'editpr.php';
        }
        if ($(this).hasClass('add')) {
            frm.action = 'addmulpr.php';
        }
        if ($(this).hasClass('remove')) {
                if (!confirm('Are you sure want to continue?')) {
                return
            }
        }
        frm.submit();
    });
});
</script>

<select id="button1">
<option value="add" selected="selected">Add</option>
<option value="edit">Edit</option>
<option value="delete">Delete</option>
</select>
<?php
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['id'] . '"'.($row['pr'] == ""?"disabled ":"").' style="cursor:pointer;" class="checkbox"></td> ?>

  <a class="button add buttons" style="cursor:pointer;"><span><b>Add Purchase Request</b></span></a>
  <a id="edit" class="button edit buttons" style="cursor:pointer;"><span><b>Edit Purchase Request</b></span></a>
  <a id="remove"class="button remove buttons" style="cursor:pointer;" name="remove"><span><b>Remove Purchase Request</b></span></a> 
  <input type='hidden' class='button' name='remove'/>
  <a href="javascript:setCheckboxes3(true);" class="button chkmenu"><span><b>Check All</b></span></a>
  <a href="javascript:setCheckboxes3(false);" class="button chkmenu"><span><b>Uncheck All</b></span></a>
<script>
$('#button1').change(function () {
    var index = $(this).find('option:selected').index();
    $('a.buttons').eq(index).show().siblings('a.buttons').hide();
}).change();
</script>

【问题讨论】:

  • 旁注:这个id="checkbox[]"不应该有括号;只有name="checkbox[]"
  • @Fred-ii- 好的,谢谢。
  • 不客气。另外,这条线&lt;?php &lt;td&gt;&lt;input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['id'] . '"'.($row['pr'] == ""?"disabled ":"").' style="cursor:pointer;" class="checkbox"&gt;&lt;/td&gt; ?&gt; 是不正确的。您需要回显 &lt;?php ... ?&gt; 中的内容,这将引发错误。
  • @Fred-ii- 是的,我附和它,我只是把它剪短了。
  • @Fred-ii- 请帮帮我?

标签: php jquery checkbox


【解决方案1】:

查看 id 命名约定 ..Limit to jQuery id strings?

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number  of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

【讨论】:

  • 这不是来自 jquery 文档,而是 HTML4 的 id 规范。 Jquery 只会对冒号和句点感到挑剔,而且只有在选择器中没有转义时才会如此。
猜你喜欢
  • 2016-01-11
  • 2020-02-25
  • 2022-01-19
  • 1970-01-01
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 2015-11-03
相关资源
最近更新 更多