如果这是你确切的标记,你可以做到这一点。还要注意,这个更新实际上可以在不同的浏览器上工作。由于您的复选框,目前没有一个ID,我使用的是兄弟选择通过其父p
标签来访问它:
jQuery(function($){ // DOM Ready
$("#passwordID + p input").click(function(){
var new_type = $(this).is(':checked') ? "text" : "password",
pwd = $("#passwordID"); // We keep replacing it, so find it again
if(pwd.attr('type') !== new_type){
pwd.replaceWith(
$("<input />", {type: new_type, value: pwd.val(), id: "passwordID", name: "PasswordName"})
);
}
}).click(); // Trigger it once on load in case browser has remembered the setting
});
Demo on JSBin
我相信这不会在IE浏览器正常工作。 – SLaks 2010-05-17 16:35:48
请确保在IE中测试它,因为它不会让你这样做,至少不是你所拥有的,你将不得不移除/添加一个输入元素,这种类型在IE中是不可变的:) – 2010-05-17 16:36:50