admin管理员组文章数量:1313091
I want to create a search field. My code so far:
<input type="text" value="Search" onfocus="if (this.value=='Search')
{this.value='';}" onblur="if (this.value=='') {this.value='Search';}"
class="textbox"/>
I want to change the <input>
tag to @Html.Editor("term").
How can I write the onfocus and onblur events using @Html.Editor("term")?
I want to create a search field. My code so far:
<input type="text" value="Search" onfocus="if (this.value=='Search')
{this.value='';}" onblur="if (this.value=='') {this.value='Search';}"
class="textbox"/>
I want to change the <input>
tag to @Html.Editor("term").
How can I write the onfocus and onblur events using @Html.Editor("term")?
- @Html.Editor("term") sends "term" text to Search action in controller. – Ceyhun Rehimov Commented Jun 14, 2012 at 6:05
- ASP.NET MVC then? You should add that as a tag. – joshschreuder Commented Jun 14, 2012 at 6:06
- <form action="@Url.Action( "Search", "Home" )" enctype="text/plain" method="get"> <input type="text" value="Search" onfocus="if (this.value=='Search') {this.value='';}" onblur="if (this.value=='') {this.value='Search';}" class="textbox" /> <input class="icon" type="image" src="images/searchicon.png" )" </form> – Ceyhun Rehimov Commented Jun 14, 2012 at 6:12
- I changed this to: @using ( Html.BeginForm( "Search", "Home", FormMethod.Get ) ) { @Html.Editor( "term") <div> <input class="icon" type="image" src="@Url.Content( "~/images/searchicon.png" )" /> </div> } – Ceyhun Rehimov Commented Jun 14, 2012 at 6:12
2 Answers
Reset to default 4Why not use HTML5 placeholder
attribute it does the same thing
Replace -
<input type="text" value="Search" onfocus="if (this.value=='Search')
{this.value='';}" onblur="if (this.value=='') {this.value='Search';}"
class="textbox"/>
With
<input type="text" class="textbox" placeholder="Search" />
Demo: http://jsfiddle/uCkef/1/
Here is how you can make 'placeholder'
attribute work in IE browsers -
http://dipaksblogonline.blogspot.in/2012/02/html5-placeholder-in-ie7-and-ie8-fixed.html
And for focus and blur events -
$('input').focus(function(){
console.log('focused');
}).blur(function(){
console.log('blured');
})
Try adding the javascript as a new html attrebute
@HTML.TextboxFor(m => m.term, new { onfocus="if (this.value=='Search') {this.value='';}" })
本文标签: javascriptHow write onfocus and onblur events for HtmlEditor()Stack Overflow
版权声明:本文标题:javascript - How write onfocus and onblur events for @Html.Editor()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741932360a2405647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论