如何禁用和启用JavaScript中的下拉列表框?

问题描述:

你好,

Hello ,

function validateRelationshipGender() {

        var gender, genderForm;
        gender = document.getElementById("NewUser:ContactPersonGender").value;
        genderForm = document.getElementById("NewUser:ContactPersonGender").disabled = true;
        if (gender == "Male" || gender == "Female") {

            genderForm = document.getElementById("NewUser:ContactPersonGender").disabled = false;

        }

    }

我建​​议使用jQuery,它使JavaScipting的编写和读取更加容易:

要使用jQuery禁用控件,请使用attr()函数,将属性作为第一个参数,将值作为第二个参数.

I would recommend using jQuery, it makes JavaScipting easier to write and read:

To disable a control using jQuery use the attr() function passing in the attribute as the first parameter and value as the second.


(" ).attr(" > true );
("#ddlc").attr("disabled", true);



其中ddlc是控件的ID(在我的情况下是ASP.NET,在您的情况下可能是JSF):



where ddlc is the id of the control (which in my case is ASP.NET and in yours probably JSF):

<asp:dropdownlist id="ddlc" runat="server" clientidmode="Static"></asp:dropdownlist>




有关jQuery详细文档,请参考: jQuery [ ^ ]

请让我知道这是否对您不起作用,我将删除此解决方案.




For detailed jQuery documentation refer: jQuery[^]

Please let me know if this doesn''t work for you and I will remove this solution.