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>
精彩评论