【问题标题】:Enabling a disabled element启用禁用的元素
【发布时间】:2017-08-24 09:48:10
【问题描述】:

我尝试在单击 P 元素时启用禁用的元素。下面的代码会将文本框中的值存储到另一个文本框中,该文本框我已附加了 div。稍后此文本框将被禁用。鼠标悬停在 div 上将出现编辑和删除。单击编辑时,必须再次启用 div 中的文本框。

<div id="ta"></div>
<input type="text" id="tb"><br>
<button onclick="add()">Submit</button><br>    

<script type="text/javascript">

var ta="";
    function add() {
        var newDiv="",newTa="",newP="",newImg="";

        ta=document.getElementById('ta');
    newDiv = document.createElement("div");

    ta.appendChild(newDiv);

    newTa = document.createElement("input");
    newTa.type="text"
        newTa.disabled="true";
        newTa.value=document.getElementById("tb").value;

        newDiv.onmousedown=function(){

        newP.style.visibility="visible";
    newImg.style.visibility="visible";  

        };


        newP=document.createElement("p");

        newP.innerHTML="Edit";
        newP.style.visibility="hidden";
        newP.style.display="inline";
        newP.style.padding="5px";
        newP.onclick=function()
        {
           newTa.disabled="false";//this is not working

        }

为什么它不起作用?还有其他方法吗?

【问题讨论】:

  • 'false'需要加引号吗?
  • 提供一个工作示例
  • BadHorsie 的回答是正确的。像这样的问题通常是由于混淆了 HTML 属性和 DOM 节点属性之间的区别,所以这里有一个很好的阅读:stackoverflow.com/questions/6003819/…
  • 天哪,我太傻了...删除引号现在可以正常工作了..谢谢

标签: javascript html


【解决方案1】:

原因可能是因为您将"false" 作为字符串提供。 From another answer here:

[...] 非空字符串是真实的。因此将“false”分配给 disabled 属性与将其设置为 true 的效果相同。

尝试使用正确的布尔值。

newTa.disabled = false;

【讨论】:

    【解决方案2】:
    newTa.disabled="true"
    newTa.disabled="false"
    

    这两个应该没有""

    newTa.disabled=true
    newTa.disabled=false
    

    否则你可以这样做:

    var x = document.getElementById("mySelect").disabled;
    

    https://www.w3schools.com/jsref/prop_select_disabled.asp

    【讨论】:

      猜你喜欢
      • 2015-07-10
      • 1970-01-01
      • 2016-07-03
      • 2019-08-10
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 2018-02-18
      • 2019-11-14
      相关资源
      最近更新 更多