简体   繁体   中英

How find the numbers in input value and hide are numbers with jQuery

i have input like this:

<input type="submit" name="starts" value=" 1154 Update "  class="btn-success">

i need a find any numbers in Value and hide this numbers like this:

<input type="submit" name="starts" value=" Update "  class="btn-success"> 

thanks for your help....

i need a find any numbers in Value and hide this numbers

You can use setAttribute

var button = document.querySelector('input[type="submit"][name="starts"]');
var value = button.getAttribute( "value" );
button.setAttribute( "value", value.replace(/\d+/g, "") ); //use trim if required

Demo

 var button = document.querySelector('input[type="submit"][name="starts"]'); var value = button.getAttribute("value"); button.setAttribute("value", value.replace(/\\d+/g, "")); //use trim if required 
 <input type="submit" name="starts" value=" 1154 Update " class="btn-success"> 

You can do something like this to replace the numbers.
I have applied both Keyup and Change events.

 $("#inputRegex").change(function() { $(this).val($(this).val().replace(/\\d+/g, '')) }); $("#inputRegex").keyup(function() { $(this).val($(this).val().replace(/\\d+/g, '')) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="inputRegex" /> 

if you want to hide those numbers on click on submit button:

$(":submit").click(function() {
  string value =  $(this).val().toString();
  value = $.trim(value.replace(/\d+/g, ""));
  $(this).val(value);

});

if you want to hide on load:

$( document ).ready(function() {
$(":submit").each(function() {
      string value =  $(this).val().toString();
      value = $.trim(value.replace(/\d+/g, ""));
      $(this).val(value);

        });
})

this will remove all the numbers in values of all input type submit. here is fiddle

HTML Part :

<input type="submit" name="starts" value=" 1154 Update " class="btn-success">
<input type="submit" name="starts" value=" 1155 Update " class="btn-success">
<input type="submit" name="starts" value=" 1156 Update " class="btn-success">

And jQuery Part :

$('input[type="submit"]').val($('input[type="submit"]').val().replace(/\d+/g, "").trim()); 

This fiddle might help you.

If you want to hide it. You can set any data attribute to the submit button

<input type="submit" name="starts" data-id ="1154" value="Update " class="btn-success">

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