简体   繁体   中英

jquery focusout on a textbox in partial view asp.net mvc

Want to fire alert when somebody leaves the textbox that is in a partial view in a asp.net mvc 4 application. Wont work for me. no error just not working. my code Jquery

$("#username").on('focusout', 'input', function () {
    alert("Lost Focus");
});

Html in the partial View

<div class="form-group">
    <label class="col-sm-6 control-label forms">UserName</label>
    <div class="col-sm-3 input-container">
        <input type="text" name="Username" id="username"/>
    </div>
</div>

The problem with your code is that you are trying to attach the event handler to an element that does not exist. (You filter your #username element's descendants with the input selector, so basically what you are saying to jquery is "bind the focusout handler to the descendants of #username that match the input selector". See documentation for more info.)

This will work:

$("#username").on('focusout', function () {
   alert("Lost Focus");
});

Try

$(document).ready(function(){
    $('#username').focusout(function(){
        alert('lost focus!');
    });
});

https://jsfiddle.net/fc4mpo1w/6/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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