最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add attributes to textbox using jQuery - Stack Overflow

programmeradmin2浏览0评论

I am using asp MVC.

I have control like

<%= Html.TextBox("username") %>

I want lost focus event to that control.

So code like

$(document).ready(function() {
  $("#username").Attributes.Add("onblur", "alert('losing focus');");
});

but it is not working,

Ultimate goal is to check password & confirm password matches

help me!

I am using asp.net MVC.

I have control like

<%= Html.TextBox("username") %>

I want lost focus event to that control.

So code like

$(document).ready(function() {
  $("#username").Attributes.Add("onblur", "alert('losing focus');");
});

but it is not working,

Ultimate goal is to check password & confirm password matches

help me!

Share Improve this question edited Mar 23, 2019 at 3:47 Shiladitya 12.2k17 gold badges28 silver badges42 bronze badges asked Apr 28, 2009 at 10:04 VikasVikas 24.3k37 gold badges118 silver badges159 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 15

It looks like you're trying to use C# code in jQuery?

The easiest way to bind an event to onblur in jQuery is:

$("#username").blur(function() { alert('losing focus'); });

More information on blur() is available at http://docs.jquery.com/Events/blur

You can try attach to this event with another way, like this:

$("#username").bind("blur", function(e){
  alert('hello');
});
$(document).ready(function() {
  $("#username").blur(function() {
    alert('byebye focus');
  });
});

http://docs.jquery.com/Events/blur

I believe your jQuery syntax is wrong,

You want to bind an event, "onBlur" that fires the alert, so try

$("#username").blur(function(){
  alert("loosing focus");
});

-- update, looks like some one answered this as I was answering

发布评论

评论列表(0)

  1. 暂无评论