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")?

Share Improve this question edited Jun 14, 2012 at 8:12 mdb 52.9k11 gold badges66 silver badges62 bronze badges asked Jun 14, 2012 at 5:58 Ceyhun RehimovCeyhun Rehimov 1112 silver badges11 bronze badges 4
  • @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
Add a ment  | 

2 Answers 2

Reset to default 4

Why 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