change type of input field with jQuery

后端 未结 29 2099
青春惊慌失措
青春惊慌失措 2020-11-22 05:13
$(document).ready(function() {
    // #login-box password field
    $(\'#password\').attr(\'type\', \'text\');
    $(\'#passwo         


        
相关标签:
29条回答
  • 2020-11-22 05:35

    I've created a jQuery extension to toggle between text and password. Works in IE8 (probably 6&7 as well, but not tested) and won't lose your value or attributes:

    $.fn.togglePassword = function (showPass) {
        return this.each(function () {
            var $this = $(this);
            if ($this.attr('type') == 'text' || $this.attr('type') == 'password') {
                var clone = null;
                if((showPass == null && ($this.attr('type') == 'text')) || (showPass != null && !showPass)) {
                    clone = $('<input type="password" />');
                }else if((showPass == null && ($this.attr('type') == 'password')) || (showPass != null && showPass)){
                    clone = $('<input type="text" />');
                }
                $.each($this.prop("attributes"), function() {
                    if(this.name != 'type') {
                        clone.attr(this.name, this.value);
                    }
                });
                clone.val($this.val());
                $this.replaceWith(clone);
            }
        });
    };
    

    Works like a charm. You can simply call $('#element').togglePassword(); to switch between the two or give an option to 'force' the action based on something else (like a checkbox): $('#element').togglePassword($checkbox.prop('checked'));

    0 讨论(0)
  • 2020-11-22 05:38

    One step solution

    $('#password').get(0).type = 'text';
    
    0 讨论(0)
  • 2020-11-22 05:39

    This will do the trick. Although it could be improved to ignore attributes that are now irrelevant.

    Plugin:

    (function($){
      $.fn.changeType = function(type) {  
        return this.each(function(i, elm) {
            var newElm = $("<input type=\""+type+"\" />");
            for(var iAttr = 0; iAttr < elm.attributes.length; iAttr++) {
                var attribute = elm.attributes[iAttr].name;
                if(attribute === "type") {
                    continue;
                }
                newElm.attr(attribute, elm.attributes[iAttr].value);
            }
            $(elm).replaceWith(newElm);
        });
      };
    })(jQuery);
    

    Usage:

    $(":submit").changeType("checkbox");
    

    Fiddle:

    http://jsfiddle.net/joshcomley/yX23U/

    0 讨论(0)
  • 2020-11-22 05:41

    Have you tried using .prop()?

    $("#password").prop('type','text');
    

    http://api.jquery.com/prop/

    0 讨论(0)
  • 2020-11-22 05:41

    Type properties can't be changed you need to replace or overlay the input with a text input and send the value to the password input on submit.

    0 讨论(0)
  • 2020-11-22 05:41

    heres a DOM solution

    myInput=document.getElementById("myinput");
    oldHtml=myInput.outerHTML;
    text=myInput.value;
    newHtml=oldHtml.replace("password","text");
    myInput.outerHTML=newHtml;
    myInput=document.getElementById("myinput");
    myInput.value=text;
    
    0 讨论(0)
提交回复
热议问题