简体   繁体   English

在不使用cloneNode的情况下将输入类型从IE中的密码更改为文本

[英]Changing input type to text from password in IE without using cloneNode

My use case is to display a textbox for entering a credit card number and by default it should be masked. 我的用例是显示一个用于输入信用卡号的文本框,默认情况下应将其屏蔽。 And then I would have a checkbox, clicking on which the above text box should unmask the creditCardNumber. 然后,我将得到一个复选框,单击上面的文本框可以取消屏蔽creditCardNumber。

I am able to achieve this using jQuery, however I am running into Spring binding issues whenever I have the checkbox checked. 我可以使用jQuery来实现此目的,但是,每当选中此复选框时,我都会遇到Spring绑定问题。 It is not taking the latest value when the checkbox is checked. 选中复选框时,它不会采用最新值。

On change of the checkbox I am calling this from ready function: 更改复选框后,我从ready函数调用此函数:

    $("#showCardNumber").change (function(){
        unmaskCreditCardTxtBox($(this)); 
    });


function unmaskCreditCardTxtBox(jqCheckBox){
$("#creditCardNumber").each(
    function(){
        var oldbox = $(this);
        var boxtype = 'password';
        if(checkBoxIsChecked(jqCheckBox)) {
            boxtype = 'text';
        }
        swapCreditCardTextBox(oldbox, boxtype);
    }
);
}



 function swapCreditCardTextBox(oldbox, boxtype){
    var newbox = $("<input></input>");
    newbox.attr('id',oldbox.attr('id'));
    newbox.attr('name',oldbox.attr('name'));
    newbox.attr('type',boxtype);
    newbox.attr('maxlength', oldbox.attr('maxlength'));
    newbox.attr("class", oldbox.attr("class"));
    newbox.val(oldbox.val());
    newbox.attr("className",oldbox.attr("className"));
    oldbox.replaceWith(newbox);
}

function checkBoxIsChecked(jqCheckBox) {
    return jqCheckBox.is(":checked");
}

The reason is not taking the value is because you are creating a whole new object to replace the old one. 不采用该值的原因是因为您正在创建一个全新的对象来替换旧的对象。 All you need is: 所有你需要的是:

$('#creditCardNumber').get(0).type = 'text';

You can handle this in your change event for the checkbox. 您可以在复选框的change事件中处理此问题。

It will also work if you simply do: 如果您只需执行以下操作,它也将起作用:

$('#creditCardNumber').attr('type', 'text');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM