回答
要刪除th Ë默認的文本,在點擊元素:
$('input:text').click(
function(){
$(this).val('');
});
我會,不過,建議使用focus()
代替:
$('input:text').focus(
function(){
$(this).val('');
});
這是爲了響應鍵盤事件太(tab鍵,例如)。此外,您可以在元素使用placeholder
屬性:
<input type="text" placeholder="default text" />
這將清除元素的焦點,而重新出現,如果該元素爲空/用戶不輸入任何東西。
更新:我從字面上理解了「點擊」,這對我來說有點愚蠢。如果您還希望在用戶選中輸入時發生該操作(可能性很大),則可以用focus
代替click
。
更新2:我的猜測是你正在尋找佔位符;最後見註解和例子。
原來的答覆:
你可以這樣做:
$("selector_for_the_input").click(function() {
this.value = '';
});
...但是,將清除文本,無論它是什麼。如果你只是想清除它,如果它是一個特定值:
$("selector_for_the_input").click(function() {
if (this.value === "TEXT") {
this.value = '';
}
});
因此,舉例來說,如果輸入有id
,你可以這樣做:
$("#theId").click(function() {
if (this.value === "TEXT") {
this.value = '';
}
});
或者,如果它的形式與id
(比方說,「myForm會」),並要爲每一個表單域做到這一點:
$("#myForm input").click(function() {
if (this.value === "TEXT") {
this.value = '';
}
});
您可能還可以與代表團做到這一點:
$("#myForm").delegate("input", "click", function() {
if (this.value === "TEXT") {
this.value = '';
}
});
它使用delegate
掛鉤窗體上的處理程序,但將其應用於窗體上的輸入,而不是將處理程序連接到每個單獨的輸入。
如果你正在試圖做的佔位符,還有比這更給它,你可能想找到一個好的插件來做到這一點。但這裏的基本知識:
HTML:使用
<form id='theForm'>
<label>Field 1:
<input type='text' value='provide a value for field 1'>
</label>
<br><label>Field 2:
<input type='text' value='provide a value for field 2'>
</label>
<br><label>Field 3:
<input type='text' value='provide a value for field 3'>
</label>
</form>
JavaScript的jQuery的:
jQuery(function($) {
// Save the initial values of the inputs as placeholder text
$('#theForm input').attr("data-placeholdertext", function() {
return this.value;
});
// Hook up a handler to delete the placeholder text on focus,
// and put it back on blur
$('#theForm')
.delegate('input', 'focus', function() {
if (this.value === $(this).attr("data-placeholdertext")) {
this.value = '';
}
})
.delegate('input', 'blur', function() {
if (this.value.length == 0) {
this.value = $(this).attr("data-placeholdertext");
}
});
});
當然,你也可以使用從HTML5的新placeholder
attribute,只有做以上如果你的代碼運行在不支持它的瀏覽器上,在這種情況下,你想反轉我上面使用的邏輯:
HTML:
<form id='theForm'>
<label>Field 1:
<input type='text' placeholder='provide a value for field 1'>
</label>
<br><label>Field 2:
<input type='text' placeholder='provide a value for field 2'>
</label>
<br><label>Field 3:
<input type='text' placeholder='provide a value for field 3'>
</label>
</form>
的JavaScript與jQuery:(榮譽給diveintohtml5.ep.io爲placholder
feature-detection code)
jQuery(function($) {
// Is placeholder supported?
if ('placeholder' in document.createElement('input')) {
// Yes, no need for us to do it
display("This browser supports automatic placeholders");
}
else {
// No, do it manually
display("Manual placeholders");
// Set the initial values of the inputs as placeholder text
$('#theForm input').val(function() {
if (this.value.length == 0) {
return $(this).attr('placeholder');
}
});
// Hook up a handler to delete the placeholder text on focus,
// and put it back on blur
$('#theForm')
.delegate('input', 'focus', function() {
if (this.value === $(this).attr("placeholder")) {
this.value = '';
}
})
.delegate('input', 'blur', function() {
if (this.value.length == 0) {
this.value = $(this).attr("placeholder");
}
});
}
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
+1爲徹底性...... =) – 2011-04-25 12:56:47
$("input:text").focus(function(){$(this).val("")});
如果需要佔位樣的行爲你可以試試這個
$('#myFieldID').focus(function(){
$(this).val('');
});
。你可以使用這個。
$("selector").data("DefaultText", SetYourDefaultTextHere);
// You can also define the DefaultText as attribute and access that using attr() function
$("selector").focus(function(){
if($(this).val() == $(this).data("DefaultText"))
$(this).val('');
});
我想你正在嘗試創建一個效果,其中的文本框包含一個標籤。當用戶點擊文本框時,它會消失,並讓用戶輸入文本。你不需要Jquery。
<input type="text" value="Input Text" onfocus="this.value=''" onblur="(this.value=='')? this.value='Input Text':this.value;" />
有一種優雅的方式來做到這一點:
<input type="text" value="Search" name=""
onfocus="if (this.value == 'Search') {this.value = '';}"
onblur="if (this.value == '') {this.value = 'Pesquisa';}"/>
這樣,如果你鍵入搜索框裏面任何東西,它不會被點擊時,裏面刪除箱子再次。
enter code here<form id="form">
<input type="text"><input type="text"><input type="text">
<input type="button" id="new">
</form>
<form id="form1">
<input type="text"><input type="text"><input type="text">
<input type="button" id="new1">
</form>
<script type="text/javascript">
$(document).ready(function(e) {
$("#new").click(function(){
//alert("fegf");
$("#form input").val('');
});
$("#new1").click(function(){
//alert("fegf");
$("#form1 input").val('');
});
});
</script>
這是什麼補充說,其他答案沒有? – ClickRick 2014-05-11 20:47:07
試試這個
$("input[name=search-mini]").on("search", function() {
//do something for search
});
function submitForm() { if (testSubmit()) { document.forms["myForm"].submit(); //first submit document.forms["myForm"].reset(); //and then reset the form values } } </script> <body> <form method="get" name="myForm"> First Name: <input type="text" name="input1"/> <br/> Last Name: <input type="text" name="input2"/> <br/> <input type="button" value="Submit" onclick="submitForm()"/> </form>
我建議,因爲我有一個固定得到了同樣的問題,使用此。
$('input:text').focus(
function(){
$(this).val('');
});
你應該添加一些解釋。我們都想知道你的解決方案爲什麼更好。 – 2016-10-24 14:29:45
這只是可接受解決方案的複製。 – David 2016-10-24 19:47:32
- 1. 如何清除輸入字段Angularjs一個按鈕,點擊後
- 2. 如何清除easyui combobox輸入點擊
- 3. 查詢清除輸入字段點擊
- 4. asp點擊輸入字段清除值
- 5. 如何在點擊後刪除文字?
- 6. 輸入字段被清空後如何刪除輸入字段
- 7. 輸入類型=「文件」,點擊取消後在文件中清除文件
- 8. 在處理完輸入後清除文本輸入字段
- 9. 用Jquery點擊清除輸入框
- 10. 點擊提交時清除輸入
- 11. 單擊事件後清除輸入值
- 12. 清除另一個文本輸入字段後清除
- 13. Edittext輸入密碼,清除焦點後隱藏文字android
- 14. 清除輸入值,然後重點
- 15. 點擊後清除變量
- 16. Angular,點擊輸入後如何關注點擊輸入
- 17. 清除所有輸入和點擊後選擇數據按鈕
- 18. 如何在輸入按下後在edittext中清除字符
- 19. 如何清除焦點上的文字輸入但仍能夠輸入? Flash/AS2
- 20. 如何清除點擊textarea?
- 21. 如何清除輸入:元素後
- 22. 如何清除點擊文本框?
- 23. 如何在首字母后清除輸入值?
- 24. 使用鏈接點擊清除輸入字段Javascript
- 25. Phonegap清除輸入焦點
- 26. 清除按鈕,輸入文件名點擊
- 27. 清除textarea包含點擊或輸入文本時的說明?
- 28. 點擊清除輸入搜索文本不起作用
- 29. AngularJS - Textarea不想在點擊後清除
- 30. 如何清除TextInputDialog的輸入字段?
我認爲Val中的V應該是小寫。 ;-) – scunliffe 2011-04-25 11:02:06
它應該,你是絕對正確的。 ... dammit,iPhone ... – 2011-04-25 11:09:42
@David:LOL回答您的iPhone上的問題。這是專用的。 – 2011-04-25 11:11:13