admin管理员组

文章数量:1355731

I want to replace in input this / by this m..

My input looks like:

<input type="text" class="text" name="address1" id="address1" value=""/>

In input I write something like street_name 5/47 but in datebase I want it like street_name 5 m. 47

I try already:

var replit = $('input[name=address1]').val().replace("/"," m. ");
$('input[name=address1]').val(replit);

And

$('input[name=address1]').val(function(index, value) {
     return value.replace('/', ' m. ');
});

But this is not work form me, I'm new in programming, please give me any tips.

I want to replace in input this / by this m..

My input looks like:

<input type="text" class="text" name="address1" id="address1" value=""/>

In input I write something like street_name 5/47 but in datebase I want it like street_name 5 m. 47

I try already:

var replit = $('input[name=address1]').val().replace("/"," m. ");
$('input[name=address1]').val(replit);

And

$('input[name=address1]').val(function(index, value) {
     return value.replace('/', ' m. ');
});

But this is not work form me, I'm new in programming, please give me any tips.

Share Improve this question edited Apr 20, 2016 at 19:04 Zakaria Acharki 67.5k15 gold badges78 silver badges106 bronze badges asked Apr 20, 2016 at 18:40 Agnes TomAgnes Tom 3564 silver badges23 bronze badges 0
Add a ment  | 

7 Answers 7

Reset to default 2

You could use input event to track user change inside the input field, then every time the user change in field the event will replace the slash / like a charm by m..

Snippet

$('body').on('input', 'input[name=address1]', function() {
  $(this).val($(this).val().replace('/', ' m. '));
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="text" name="address1" id="address1" value=""/>

Or if you don't want from the user to see the replacement you do, you could replace the slash just after the submit in submit event :

$('body').on('submit', 'your_form_selector', function() {
     $(this).val($(this).val.replace('/', ' m. '));
});

Hope this helps.


Try

$('input[name=address1]').val($('#input-field-id').val().replace('/', ' m. '))

Use this

$('#address1').on('change',function()
{  
    $(this).val($(this).val().replace("/"," m. "));
});

try this

$(document).ready(function(){
    $('input[name=preco]').blur(function(){
        var value = $(this).val();
        $(this).val(value.replace('/','m. '));
    });
});

Here is a fiddle that show your solution working: https://jsfiddle/e7n38wjz/2/ Please let me know if it is what you want. Do you know what JQuery version you are using? This JSFiddle was set up to use the JQUERY 1.6.4.

Here is the code:

<input type="text" class="text" name="address1" id="address1" value="street_name 5/47"/>
<br>
<input type="text" class="text" name="address2" id="address1" value=""/>

// JS
var replit =  $('input[name=address1]').val().replace("/"," m. ");
$('input[name=address2]').val(replit);

Both of your examples work. See here: https://jsfiddle/zreeLzo4/ and here: https://jsfiddle/q6awd8ng/

So you need to better describe what "not working" means to you. What happens that you don't expect? What didn't happen that you did expect?

One thing to note: using replace on a string will only replace the first instance of the match if you provide a string as the matcher instead of a regex. Use a regex with a global flag to replace all instances:

var startString = 'street_name 5/47 something/else';
var endString = startString.replace(/\//g," m. ");
console.log(endString); // <- street_name 5 m. 47 something m. else

If you want that only the entry in database should use 'm' instead of '/', then you should consider changing the input value wherever you are writing this data to database. You should consider making use of the .submit() event in the following way :

<script>
    $(document).ready(function(){
        $("#detailForm").on("submit", function(){
            var value = $('input[name=address1]').val();
            value = value.replace("/", " m. ");
            $('input[name=address1]').val(value);
        });
    });
</script>

本文标签: javascriptInput str replaceStack Overflow