jquery 如何禁用 td

jquery how to disable a td

我有以下table

 <c:forEach items="${tablaserie.matriculas}" var="matricula" varStatus="loopmatricula">
    <input type="hidden" id="<c:out value="matricula-${loopmatricula.index}" />" value="<c:out value="${matricula.matricula}" />" />
    <tr>
    <td style="text-align:center"><c:out value="${matricula.matricula}" /></td>
        <c:set var="numeroceldas" value="[=11=]" />
        <c:forEach items="${matricula.listado}" var="celda" varStatus="loopcelda">
            <c:set var"identificadorcelda" value="${matricula.matricula}-${loopcelda.index}" />

            <c:choose>
                <c:when test="${celda.color eq '#ffffff' }" >       
                    <td  id="td-${identificadorcelda}" style="text-align:center;" onclick="displayCombo();" ><c:out value="${celda.nombre}" /></td>     
                    <input id="input-${identificadorcelda}" type="hidden" value="<c:out value="${celda.nombre}" />" />              
                </c:when>
                <c:otherwise>
                    <td  id="td-${identificadorcelda}" style="color:white;text-align:center;" bgcolor="<c:out value="${celda.color}"/>"  onclick="displayCombo();">
                        <c:out value="${celda.nombre}" />
                    </td>
                    <input id="input-${identificadorcelda}" type="hidden" value="<c:out value="${celda.nombre}" />" />      

                </c:otherwise>
            </c:choose>
    /c:forEach>
</tr>
</c:forEach>

我想禁用 tdtd 的名字是

 id="td-${identificadorcelda}"

$(document).ready()我有

if (tipoedicion == 0){
            $("td[id*=td]").prop('disabled', true);

        }
        else {
            $("td[id*=td]").prop('disabled',false);
        }

td 没有禁用,我可以点击它们。

如何禁用 td?

td 没有禁用属性。它用于输入元素。您可以将鼠标光标隐藏在 td 上并禁用选择。它会让它像禁用一样工作

$(document).ready(function() {
  $("#a").prop("disabled", true);
  $('#b').css({
    'cursor': 'none',
    'user-select': 'none'
  });
  $('#b').click(function(){return false})

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <td id="b">sss</td>
</table>
<input id="a">

因为 td 并没有真正的 disabled 属性,如果你想阻止 click 事件做它做的事,你可以设置自定义 data-disabled 改为 td 的属性。

所以在你的$(document).ready()中你可以做这样的事情

if (tipoedicion == 0){
        $("td[id*=td]").attr('data-disabled', 'true');

    } else {
        $("td[id*=td]").attr('data-disabled', 'false');
    }
    // ...
}

然后,在您的 displayCombo() 函数中,您可以检查该属性,如果 td 将该属性设置为 "true",则只需 return,就像这样[=21] =]

function displayCombo() {
    if ($("td[id*=td]").attr('data-disabled') === 'true') {
        return;
    }
    // whatever it was doing before
}