change type of input field with jQuery

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


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

    Simple solution for all those who want the functionality in all browsers:

    HTML

    <input type="password" id="password">
    <input type="text" id="passwordHide" style="display:none;">
    <input type="checkbox" id="passwordSwitch" checked="checked">Hide password
    

    jQuery

    $("#passwordSwitch").change(function(){
        var p = $('#password');
        var h = $('#passwordHide');
        h.val(p.val());
        if($(this).attr('checked')=='checked'){
            h.hide();
            p.show();
        }else{
            p.hide();
            h.show();
        }
    });
    
    0 讨论(0)
  • 2020-11-22 05:29

    Here is a little snippet that allows you to change the type of elements in documents.

    jquery.type.js (GitHub Gist):

    var rtype = /^(?:button|input)$/i;
    
    jQuery.attrHooks.type.set = function(elem, value) {
        // We can't allow the type property to be changed (since it causes problems in IE)
        if (rtype.test(elem.nodeName) && elem.parentNode) {
            // jQuery.error( "type property can't be changed" );
    
            // JB: Or ... can it!?
            var $el = $(elem);
            var insertionFn = 'after';
            var $insertionPoint = $el.prev();
            if (!$insertionPoint.length) {
                insertionFn = 'prepend';
                $insertionPoint = $el.parent();
            }
    
            $el.detach().attr('type', value);
            $insertionPoint[insertionFn]($el);
            return value;
    
        } else if (!jQuery.support.radioValue && value === "radio" && jQuery.nodeName(elem, "input")) {
            // Setting the type on a radio button after the value resets the value in IE6-9
            // Reset value to it's default in case type is set after value
            // This is for element creation
            var val = elem.value;
            elem.setAttribute("type", value);
            if (val) {
                elem.value = val;
            }
            return value;
        }
    }
    

    It gets around the issue by removing the input from the document, changing the type and then putting it back where it was originally.

    Note that this snippet was only tested for WebKit browsers – no guarantees on anything else!

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

    Nowadays, you can just use

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

    But of course, you should really just do this

    <input type="password" placeholder="Password" />
    

    in all but IE. There are placeholder shims out there to mimic that functionality in IE as well.

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

    This Worked for me.

    $('#newpassword_field').attr("type", 'text');
    
    0 讨论(0)
  • 2020-11-22 05:31

    It's very likely this action is prevented as part of the browser's security model.

    Edit: indeed, testing right now in Safari, I get the error type property cannot be changed.

    Edit 2: that seems to be an error straight out of jQuery. Using the following straight DOM code works just fine:

    var pass = document.createElement('input');
    pass.type = 'password';
    document.body.appendChild(pass);
    pass.type = 'text';
    pass.value = 'Password';
    

    Edit 3: Straight from the jQuery source, this seems to be related to IE (and could either be a bug or part of their security model, but jQuery isn't specific):

    // We can't allow the type property to be changed (since it causes problems in IE)
    if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode )
        throw "type property can't be changed";
    
    0 讨论(0)
  • 2020-11-22 05:34

    I like this way, to change the type of an input element: old_input.clone().... Here is an example. There is an check box "id_select_multiple". If this is is changed to "selected", input elements with name "foo" should be changed to check boxes. If it gets unchecked, they should be become radio buttons again.

      $(function() {
        $("#id_select_multiple").change(function() {
         var new_type='';
         if ($(this).is(":checked")){ // .val() is always "on"
              new_type='checkbox';
         } else {
             new_type="radio";
         }
         $('input[name="foo"]').each(function(index){
             var new_input = $(this).clone();
             new_input.attr("type", new_type);
             new_input.insertBefore($(this));
             $(this).remove();
         });
        }
      )});
    
    0 讨论(0)
提交回复
热议问题