开发者

How to enable/Disable textbox. and also make text box blank on change of value in the drop down list

I am using jquery to do enable/disable operation.

<script type="text/javascript">
        $(document).ready(function() {
            $("#ddlUser").change(function() {
                var selectedValue = $(this)[0].value;
                if (selectedValue == "--Select User--") {
                    $("#txtUserName").removeAttr("disabled");
                    $("#txtUserName").val("");
                    $("#txtPassword").removeAttr("disabled");
                    $("#txtPassword").val("");
                    $("#txtConfirmPassword").removeAttr("disabled");
                    $("#txtConfirmPassword").val("");
                    $("#txtEmailId").removeAttr("disabled");
                    $("#txtEmailId").val("");
                }
                else {
                    $("#txtUserName").attr("disabled", "disabled");
                    $("#txtPassword").attr("disabled", "disabled");
                    $("#txtConfirmPassword").attr("disabled", "disabled");
                    $("#txtEmailId").attr("disabled", "disabled");
                }
            });
        });
</script>



<%= Html.DropDownList("ddlUsers", (SelectList)ViewData["ddlUsers"], "--Select User--",   new { id = "ddlUsers", title = "Select User" })%>

Controller data passed as

 MembershipUserCollection lstUser = Membership.GetAllUsers();

  ViewData["ddlUsers"] = new SelectList(lstUser, "UserName", "UserName")开发者_运维问答;  


 // Genereric watermarking function
jQuery.fn.waterMark = function (options) {
    var defaults = {
        activeColor: '#19254A',
        inActiveColor: '#19254A'
    };

    var settings = $.extend({}, defaults, options);

    return this.each(function () {
        var initVal = $(this).val();
        $(this).focus(function () {
            if ($(this).val() === initVal) {
                $(this).val('').css({ 'color': defaults.activeColor });
            }
            else { return false }
        });
        $(this).blur(function () {
            if ($(this).val() === '' || $(this).val() === initVal) {
                $(this).val(initVal).css({ 'color': defaults.inActiveColor });
            }
            else { return false }
        });
    });
}

and then you call it like [you must give an initial value to your text inputs like Type Search...]:

 $('input[type=text]').waterMark();

and for enable and disable:

$('input').attr('disabled', 'disabled');

and

$('input').attr('disabled', '');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜