有人知道如何使用jquery删除输入占位符吗?
我想做的是,如果其中一个获得了值...所有的占位符都将被删除...有人知道如何做到这一点吗?
<input type="text" name="name" placeholder="1.)">
<input type="text" name="age" placeholder="2.)">
<input type="text" name="address" placeholder="3.)">
有人知道如何使用jquery删除输入占位符吗?
我想做的是,如果其中一个获得了值...所有的占位符都将被删除...有人知道如何做到这一点吗?
<input type="text" name="name" placeholder="1.)">
<input type="text" name="age" placeholder="2.)">
<input type="text" name="address" placeholder="3.)">
应该有效:
$(':input').removeAttr('placeholder');
var inputs = $('input[type="text"]');
inputs.on('change', function() {
if ( $.map(inputs, function(el) { return el.value; }).join('').length )
inputs.removeAttr('placeholder');
});
相反,可以通过设置焦点来在那一刻自动隐藏占位符:
$(element).focus();
var txtbox = $('input[type="text"]');
txtbox.change(function () {
txtbox.removeAttr('placeholder');
});
$('input[name="name"]').removeProp('placeholder');
您可以在 keyup
事件中将占位符更改为空字符串。
$("input[type=text]").keyup(function() {
$("input[type=text]").attr("placeholder","")
});
var inputs = $('input');
inputs.one('change', function(){
inputs.removeAttr('placeholder');
});
我做了更进一步的改进,以防你也需要。这段代码将删除所有文本输入框中的占位符文本,如果它们中的任何一个有值,但如果你删除了所有的值,它会把占位符文本放回去。可能不是必需的,但还是保留吧...
var $inputs = $("input:text");
$inputs.on("change", function() {
if ($.map($inputs, function(el) { if (el.value) return o.value; }).length) {
$inputs.each(function() {
this._placeholder = this.placeholder;
$(this).removeAttr("placeholder");
});
} else {
$inputs.each(function() {
this.placeholder = this._placeholder;
});
}
});
$('#name').val('');
$('#name').attr('placeholder', '无论之前的占位符是什么');
尝试:
if ($(input).val()) {
$(input).attr("placeholder", "")
}
$(input)
不会选择任何输入,除非 input
是一个值为 "input"
(或类似)的字符串,并且您不能在集合上使用 val()
- 只能在单个对象上使用。 - Reinstate Monica Cellio