gpt4 book ai didi

javascript - 禁用控件

转载 作者:行者123 更新时间:2023-11-28 16:15:08 24 4
gpt4 key购买 nike

我正在尝试禁用 Aspx 页面上的某些控件,但无法使其正常工作。我尝试过 Javascript 和 JQuery,所以我知道我一定做错了一些简单的事情。

这是我的自动取款机,我知道它肯定会在我想要的时候进入这两个功能,因为我已经陷入了警报来告诉我这一点。然而,控件始终保持启用状态!

<script language="javascript" type="text/javascript">

function fun1(){
var input = $('#txtDocs');
input.removeAttr('disabled');
}

function fun2() {
var input = $('#txtDocs');
input.attr('disabled', 'disabled');
}

以及控件本身;

<p>
<asp:TextBox ID="txtDocs" runat="server" Width="218px"
Height="75px" TextMode="MultiLine" MaxLength="40"/>
</p>

我还尝试将两个函数中的代码替换为以下各种版本;

document.getElementById("txtDocs").enabled = true;
document.getElementById("txtDocs").attributes.enabled = true;
document.getElementById("txtDocs").disabled = false;

虽然没有什么好处。我做错了什么?

更新

当前代码;

function fun1(){
alert("1 enable");
$('#txtDocs').removeAttr('disabled');
alert("2 enable");
}

function fun2() {
alert("1 disable");
$('#txtDocs').prop('disabled', true);
alert("2 disable");
}

<asp:TextBox ID="txtDocs" runat="server" Width="218px" ClientIDMode="Static"
Height="75px" TextMode="MultiLine" MaxLength="40"/>

还是不行:/我只看到alert("1disable")alert("1enable")alert("2启用”) 所以无法通过 fun2...

注意

调试我的代码,只是注意到在运行时我的文本框实际上是一个文本区域。有什么意义吗?此外,它的 ID 将在开头标记通常的“ctl00_MainContentPlaceHolder_”。

最佳答案

如果您使用的是 jQuery 1.6+,则应该使用 prop()

$('#txtDocs').prop('disabled',true);
$('#txtDocs').prop('disabled',false);

根据 jQuery 文档 http://api.jquery.com/prop/

The .prop() method should be used to set disabled and checked instead of the .attr() method.

关于javascript - 禁用控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11742794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com