开发者

Javascript onBlur prefix negative sign

开发者 https://www.devze.com 2023-01-21 17:30 出处:网络
How do I开发者_JAVA技巧 prefix a - sign for all the values entered in a textbox in javascript onblurFirst, you\'ll need to add the event handler.Once you do that, this is your function:

How do I开发者_JAVA技巧 prefix a - sign for all the values entered in a textbox in javascript onblur


First, you'll need to add the event handler. Once you do that, this is your function:

function textBoxBlur(e) {
    var evt = e || window.event;
    var tb = evt.target || evt.srcElement
    if (tb.value.indexOf('-') === -1) {
        tb.value = '-' + tb.value;
    }
}


Usign jquery you can:

$(selector).blur(function (){
  $(this).val("-"  +$(this).val());
});

With bare javascript is easy too:

document.getElementById("a").onblur = function (){
  this.value = "-" + this.value;
}

Don't forget to bind this events after window loads.

window.onload = function (){
  /* here */
}


If you use jQuery, which I highly recommend, you can:

$('#id-of-textbox').bind('blur', function() {
    $(this).val('-' + $(this).val());
});

Put this in a <script> tag at the end of your html.

With jQuery you can now add this functionality to any textbox you want this behavior. Go http://jquery.com/ for more info.

0

精彩评论

暂无评论...
验证码 换一张
取 消