javascript 如何在单击按钮时启用/禁用文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10935889/
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 11:32:18  来源:igfitidea点击:

How to enable/disable a text box on clicking a button

javascripthtml

提问by Coding active

For example my page has a text box which already has some value and the readonly option for it is "true" , when the edit button is clicked next to the text box , the editing option in text box is enabled. How do I implement enabling the textbox when i click on a button.

例如我的页面有一个文本框,它已经有一些值,它的只读选项是“true”,当单击文本框旁边的编辑按钮时,文本框中的编辑选项被启用。单击按钮时如何实现启用文本框。

function func() {
    $("input:button[name='button1']").click(function() {
        $("#text11").val($(this).val()).attr("disabled", "disabled");
        if($(this).val() != "") { 
            $("#text11").attr("disabled", "").focus();
        } 
    });
}
<input type="text" name="text11" readonly="readonly" value="Editing is disabled">
<input type="button" value="edit" name="button1">

which is when I click this button , the readonly option in textbox should be disabled.How do I do that ?

这是当我单击此按钮时,应禁用文本框中的只读选项。我该怎么做?

回答by Jamie Hutber

basically you just want to add the attribute disabled to your element to stop users using it.

基本上,您只想将禁用的属性添加到您的元素以阻止用户使用它。

$('#disablebutton').click(function(){
    $('#textfieldToClose').attr('disable');
});

<input type="text" name="text11" readonly="readonly" id="textfieldToClose">
<input type="button" value="edit" name="button1" id="disablebutton">

i believe this should work, haven't tested it mind.

我相信这应该有效,还没有测试过。

回答by 20911141

You need to use HTML DOM objects...

您需要使用 HTML DOM 对象...

http://www.w3schools.com/jsref/default.asp

http://www.w3schools.com/jsref/default.asp