【发布时间】:2009-02-12 11:07:39
【问题描述】:
我有一个输入,它的类型设置为隐藏,我需要将它的类型更改为文本。似乎无法弄清楚这一点,或者是否可以使用 jQuery
【问题讨论】:
标签: javascript jquery html css
我有一个输入,它的类型设置为隐藏,我需要将它的类型更改为文本。似乎无法弄清楚这一点,或者是否可以使用 jQuery
【问题讨论】:
标签: javascript jquery html css
使用 jQuery 1.4,您可以在输入分离时更改输入的类型。
marker = $('<span />').insertBefore('#myInput');
$('#myInput').detach().attr('type', 'text').insertAfter(marker);
marker.remove();
【讨论】:
我不确定这是否可行,所以我建议使用隐藏的 div 来包含输入元素,它可以根据需要显示。即使隐藏,文本输入字段仍然可以保存数据。
【讨论】:
出于安全原因,IE 不允许您更改表单元素的类型,所以我会采用 Aram 的隐藏 div 建议。
【讨论】:
我会这样做:
$("input[type='hidden']").each(function(){
var name = $(this).attr('name'); // grab name of original
var value = $(this).attr('value'); // grab value of original
/* create new visible input */
var html = '<input type="text" name="'+name+'" value="'+value+'" />';
$(this).after(html).remove(); // add new, then remove original input
});
如果要过滤一些元素,在each()之前调用filter(),像这样:
$("input[type='hidden']").filter("[name='whatever']").each(function...
【讨论】:
简单的人......
$('input[type="hidden"]').each (function() { this.type = 'text'; });
在事件上触发它
【讨论】:
Overlay.toAnyType = function (c, type) {
var shadow = jQuery(document.createElement(c.context.nodeName));
// Clone attributes to new object.
for (var i = 0; i < c[0].attributes.length; i++) {
var attr = c[0].attributes[i].name;
var val = c[0].attributes[i].value;
if (attr == "type") val = type;
shadow.attr(attr, val);
}
shadow.insertAfter(c);
c.remove();
};
当 c 是 jQuery 对象时;上面的函数改变了类型属性。用法:
var inputElemt = jQuery("#input");
Overlay.toAnyType(inputElemt, "text");
【讨论】:
旧线程,但我最近需要做一些类似的事情,但需要使用文件字段并清除它们......我认为可以应用相同的解决方案,但至少您可以在这种情况下获取内容。
var temp = $('#hidden-field').val();
$("#container-for-field").html("<input id='not-hidden' type='text' value='"+temp+"'/>");
这也应该可以解决问题:)。
【讨论】: