Remove a given string from an input value using jQuery

前端 未结 3 1577
我在风中等你
我在风中等你 2021-01-25 08:32

I have a hidden field with three integer values, example:


I want to use jQuery to remove a giv

相关标签:
3条回答
  • 2021-01-25 08:57

    Basically the same as @Shaz answer but perhaps a little cleaner. This uses a function that returns the val to use. See the API docs at http://api.jquery.com/val/

    $('input').val(function(index, val){
        return val.replace('10', '').trim();
    });
    

    To handle the possible trailing space after your integer:

    val.replace(/10 ?/, '').trim()
    
    0 讨论(0)
  • 2021-01-25 09:10
    <input type="hidden" id="my_input" value = "10 23 42">
    <script type="text/javascript">
    jQuery(document).ready(function(){
         $("my_input").attr("value", $("my_input").attr("value").replace("10 ", ""));
    });
    </script>
    
    0 讨论(0)
  • 2021-01-25 09:13
    <script>
        $("input").val($("input").val().replace("10 ", ""));
    </script>
    
    0 讨论(0)
提交回复
热议问题