简体   繁体   中英

how to mask textbox after ajax success

I am returning values from the database after checking if it has value. If it has a value, it will be displayed in my textbox. But I want it to be masked with the format of (000-000-000-000), apparently, it is not working. But with normal input, the mask is working just fine. It's just that after success, it does not work anymore. Any help will be fine.

My code:

//..start of ajax code....
success: function(result) {
  console.log(result);
  if (result != "") {
    $("#tin").mask("000-000-000-000");
    $("#tin").val(result);
  }
}

The mask plugin formats the input text when typing (exactly oninput event )

After setting the value with ajax the format will not be applied to this last (because of the onchange event not oninput ) .

So to workaround this just trigger the input event after setting the value using the jQuery trigger() function.

Like below :

success: function(result) {
  console.log(result);
  if (result != "") {
    $("#tin").unmask().mask("000-000-000-000");
    $("#tin").val(result).trigger("input");;
  }
}

You can see the Sample Fiddle example here Link to fiddle

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