javascript - 根据单元格属性阻止表格单元格 - Javascript

标签 javascript jquery dom

我有两张 table 。一种表示月份中的天数 (Dofm_Table),另一种表示月份 (Month_Table)。对于每个月份单元格,我添加了属性“数据测试”,其中包含该特定月份的天数。

我正在尝试添加一个函数,该函数将 Month_Table 中所选单元格的“data-test”属性与 Dofm_Table 中每个单元格的“value”属性进行比较。用户不应单击 Dofm_table 中“value”属性高于 Month_Table 中所选单元格的“data-val”属性的任何单元格。

例如,如果用户单击 Month_Table 中的 Feb,则他们应该只能单击 Dofm_Table 上的单元格 1-28。

Here is an example of the attributes of each cell in Month_Table

Here is an example of the attribute of each cell in the Dofm_Table

这是我到目前为止所做的事情:

 function MonthDofmBlocks() {

        //check if dofm checkboxes has been checked. If so, block the days calender. Vice versa.
        var days = 0;
        var checkboxMonthCells = document.querySelectorAll('#Month_Table td');

        checkboxMonthCells.forEach(c => c.addEventListener('click', event => {

            days = 0;

            for (var i = 0; i < checkboxMonthCells.length; i++) {

                var NofD = parseInt($(checkboxMonthCells[i]).attr('data-test'));

                if (checkboxMonthCells[i].onclick) {
                    if (days < NofD)
                        days = NofD;
                }
            }
            var DofM = $("#Dofm_Table td").each(function (index, value) {
                var day = DofM.attr("value") + 1;

            });


        }));

    }
    MonthDofmBlocks();

这是 JSFiddle:https://jsfiddle.net/cr1aL26v/5/

最佳答案

如果您只想禁止选择某一天,您可以在点击特定日期时检查所选月份的限制。这是一个简短的示例

$("#Dofm_Table td").click(function (event) {
  var valueOfDay = parseInt($(this).attr("value"));

  //All months have at least 28 days, so we can just allow any day below that.
  if (valueOfDay > 27) {
    //find the month cell that is selected and read its value.
    var monthDayLimit = parseInt($("#Dofm_Table td.on").attr("data-test"));

    if (valueOfDay > monthDayLimit) {
        return false;
    }
  }

  $(this).toggleClass('on');
});

关于javascript - 根据单元格属性阻止表格单元格 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51671166/

相关文章:

javascript - 从具有延迟的数组顺序执行生成(jquery)

php - 使用 jQuery 从多个表单字段创建 JSON 对象

php - 有没有办法(在 PHP 中)检测脚本是否被 jQuery 加载调用?

javascript - 我可以使用 Polymer 数据绑定(bind)更改元素标签名称吗?

javascript - Knockout 和 coffeescript - uncouched 类型错误

javascript - 查找最近的元素,而不是最近的元素

java - 将序列化 java 对象保存到 Db 时出现问题

javascript - 内存中的 HTML 元素和插入到 DOM 后的显示问题

javascript 未检测到链接的点击

javascript - 当外部脚本为空时,我需要关闭 colorbox