简体   繁体   English

如何在输入值中找到数字并使用jQuery隐藏数字

[英]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: 我需要在“ Value查找任何数字并隐藏此数字,如下所示:

<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 您可以使用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. 我已经应用了KeyupChange事件。

 $("#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. 这将删除所有输入类型Submit的值中的所有数字。 here is fiddle 这是小提琴

HTML Part : HTML部分:

<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 : 和jQuery部分:

$('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">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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