简体   繁体   English

输入表单的jquery占位符文本

[英]jquery placeholder text for input form

I'm using jquery placeholder text for a search placeholder. 我正在使用jquery占位符文本作为搜索占位符。 This search field doesn't have any placeholder so i added this text. 此搜索字段没有任何占位符,因此我添加了此文本。

$(document).ready(function(){ 
   $('.mfilter-search').find("input[type=text]").each(function(ev)
     {
       if(!$(this).val()) {
       $(this).attr("placeholder", "Refine Your Search");
     }
   });
});

i don't know it's the best way to add a placeholder using jquery. 我不知道这是使用jquery添加占位符的最佳方法。 If anyone know more simple way please add your code it will be very helpful. 如果有人知道更简单的方法,请添加您的代码,这将非常有帮助。

You can directly set the placeholder no need to check value and use .each() 您可以直接设置placeholder无需检查value并使用.each()

$('.mfilter-search input[type=text]').attr("placeholder", "Refine Your Search")

If you want to place on first then use 如果你想先放置然后使用

$('.mfilter-search input[type=text]:first').attr("placeholder", "Refine Your Search")

您可以使用value属性简单地使用find方法

$('.mfilter-search').find("input[type=text][!value]").attr("placeholder", "Refine Your Search");

you can add placeholder attribute directly in your html element like this 你可以直接在你的html元素中添加占位符属性

<input type="text" name="fname" placeholder="search">

or you can add a class to all your element that you need to put placeholder eg:- .placeholder and add placeholder attribute in javascript like this. 或者你可以为你需要放置占位符的所有元素添加一个类,例如: - .placeholder并在这样的javascript中添加占位符属性。

$(".placeholder").attr("placeholder", "Type here to search");

or you can use plugins like these. 或者您可以使用这些插件。 http://andrewrjones.github.io/jquery-placeholder-plugin/ or https://github.com/mathiasbynens/jquery-placeholder or refer this site for different placeholder plugins. http://andrewrjones.github.io/jquery-placeholder-plugin/https://github.com/mathiasbynens/jquery-placeholder或参考此站点获取不同的占位符插件。 https://www.sitepoint.com/top-5-jquery-html5-placeholder-plugins/ https://www.sitepoint.com/top-5-jquery-html5-placeholder-plugins/

and your code is also good. 而你的代码也很好。 We can do that way too. 我们也可以这样做。 hope this will work. 希望这会奏效。

Try this code 试试这个代码

 $(document).ready(function(){ 
   $('.mfilter-search').find("input[type=text]").each(function(ev)
     {
           var txt = $(this).data('placeholder');
            $(this).attr('placeholder', txt);
   });
});

对于占位符,jquery中不需要复杂的方法函数,只需使用这样的方法

<input type="text" placeholder = "Zipcode" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Zipcode'" name="zipcode" class="form-control" maxlength='10'>

try this. 尝试这个。 this is how you can add place holder if you have two input fields with same class name . 如果您有两个具有相同class name input fields ,则可以使用此方法添加place holder

 <!DOCTYPE html> <html> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> </head> <style type="text/css"> </style> <body> first name :<input type="text" name="firstname" class="tfield" id="field0"> last name :<input type="text" name="lastname" class="tfield" id="field1"> </body> <script type="text/javascript"> $(document).ready(function(){ var thelength = $(".tfield").length; for(var i=0;i<thelength;i++) { var theid = $("#field"+i); var thename = theid.attr("name"); alert(thename); if(thename == "firstname") { theid.attr("placeholder","name"); } } }); </script> </html> 

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

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