to make such a control, we use d2e0a7c5207a97f0777ebe2094aeb95f tag in html within the form. it looks like a normal textbox, but it is capable in performing site search.
让我们来看一个基本的search控件程序。
示例<html><head><title></title> <script> function show(){ s=searchform.searchfield.value; document.write(you have searched for +s); } </script></head><body> <form id=searchform> <label for=sea>search for : <label> <input type=search name=searchfield> <br><br> <input type=submit value=search onclick=show()> </form> </body></html>
这个程序将使用javascript代码在下一页上显示搜索到的文本。
您可能在使用google时注意到,默认情况下搜索框中会出现一些文本,如下所示:
示例要做到这一点,我们可以在<input>标签中使用placeholder属性。让我们看一个例子。
<html><body> <form id=searchform> <label for=sea>search for : </label> <input type=search name=searchfield placeholder=type url> <br><br> <input type=submit value=search onclick=show()> </form></body></html>
value attribute的中文翻译为:value属性to set the default value in the search box, you can use value attribute in <input> tag.
这是一个非常有趣的属性,可以在<input>标签中使用,那就是spellcheck属性。此属性可以打开或关闭拼写检查功能。如果它是on,它将在拼写错误的单词下显示红色波浪线,但如果它是off,它将简单地忽略拼写错误。默认情况下,spellcheck属性设置为false。
so, let us see how we can use this attribute.
example<html><body> <form id=searchform> <label for=sea>search for : </label> <input type=search name=searchfield><br><br> <input type=submit value=search onclick=show()> </form></body></html>
or
<html><body> <form id=searchform> <label for=sea>search for : </label> <input type=search name=searchfield spellcheck=false><br><br> <input type=submit value=search> </form></body></html>
in both the cases, the spellcheck attribute is off i.e. false. in this situation, it will ignore errors.
suppose, we entered “heaith” in the search box whereas the correct spelling is “health”. but it is not showing any wavy line under the word.
now let us make it true and see the results.
example<html><body> <form id=searchform> <label for=sea>search for : </label> <input type=search name=searchfield spellcheck=true><br><br> <input type=submit value=search> </form></body></html>
we can also restrict the minimum and maximum number of characters in a search field with the help of minlength and maxlength attributes. suppose in an example, where a user has to type/search admission number for a particular institution, it can accept minimum of 4 characters and maximum of 6 characters. but if you will not put a restriction, a user can type data of any length.
example<html><body> <form id=searchform> <label for=reg>type admission number : </label> <input type=search name=adm minlength=4 maxlength=6><br><br> <input type=submit value=search> </form></body></html>
it is showing an error on the page because the user has not entered the minimum length of data. and if you try to exceed its maximum limit, it won’t allow to type.
我们还可以通过使用size属性来控制文本框的宽度。而且,我们还可以通过使用required属性将其设置为必填字段。
示例<html><body> <form id=searchform> <label for=reg>type admission number : </label> <input type=search name=adm minlength=4 maxlength=6 size=6 required><br><br> <input type=submit value=search> </form></body></html>
观察到盒子的大小现在减小了,如果您将其留空,将显示错误。
以上就是如何在html中使用搜索输入类型?的详细内容。
