开发者

Remove a given string from an input value using jQuery

I have a hidden field with three integer values, examp开发者_JS百科le:

<input type="hidden" value="10 23 42">

I want to use jQuery to remove a given value, say "10", which would leave the remaining "23 42" value.


<script>
    $("input").val($("input").val().replace("10 ", ""));
</script>


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()


<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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜