javascript 为什么输入的 value 属性没有改变?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10346471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:33:27  来源:igfitidea点击:

Why doesn't the value attribute of the input change?

javascripthtml

提问by Mr.Rendezvous

Well I have this code in view:

好吧,我有这个代码:

<input id="CI.SiteName" type="text" value="" name="@@CI.SiteName" disabled="">

and then I doing some event that would call this function:

然后我做了一些会调用这个函数的事件:

chooseSite = function () {
    var url = "/main/Ajax/GetSiteDetail?" +
        "&cid=" + escape(idSite);

    var ajx = sendAJAX(url, true);

    ajx.onreadystatechange = function () {
        if (ajx.readyState == 4) {
            var result = ajx.responseText;      
            result = "TOP";
            document.getElementById("CI.SiteName").value = result;
        }   
    }
}

in browser it changed to "TOP" but when I inspect element with firebug, the VALUE attribute of INPUT still "", not changed.

在浏览器中它变成了“TOP”但是当我用 firebug 检查元素时,INPUT 的 VALUE 属性仍然是“”,没有改变。

回答by ThiefMaster

The valueattributeis not synced with the actual value; that's what the valuepropertyis for.

value属性与实际值不同步;这就是value物业的用途。

This is not a problem though since you'll never use .getAttribute('value')but use the property .valueto access the current value.

这不是问题,因为您永远不会使用.getAttribute('value')但使用该属性.value来访问当前值。