How to replace part of an input value in jQuery?

前端 未结 5 1961
你的背包
你的背包 2021-02-13 19:10

I\'ve been trying to figure out a way to replace part of a string in an input value, but haven\'t been able to get it working.

The input field looks li

相关标签:
5条回答
  • 2021-02-13 19:31
    $('input[name=ids]').val(function(index, value) {
       return value.replace('54,', '');
    });
    
    0 讨论(0)
  • 2021-02-13 19:34

    try

    replit = replit.replace("54,","");
    
    0 讨论(0)
  • 2021-02-13 19:42
    var replit = $('input[name=ids]').val().replace("54,","");
    $('input[name=ids]').val(replit);
    

    hope that helps :)

    EDIT: if you want a working example: http://jsfiddle.net/Damien_at_SF/XQQC2/

    :)

    0 讨论(0)
  • 2021-02-13 19:48

    I used this method, however I also included a line of code that replaces part of a string.

    var matval = document.getElementById("MaterialNumber").value;
    
    var matNum = matval.replace("P", "");
    
    document.getElementById("MaterialNumber").value = "";
    
    document.getElementById("MaterialNumber").value = matNum;
    

    Generally I would not have cleared out the field before putting in the new string, but it did not work like that in this case, This might have been caused by the input method being a barcode scanner and set into a function based on a keypress of that said barcode scanner.

    0 讨论(0)
  • 2021-02-13 19:55

    I've just tried this

    $('#title').val($('#title').val().replace("54,",""))
    

    and it worked so i assume that it's the way you call the element "$('input[name=ids]')", why don't you try using an id selector?

    $('#ids')
    
    0 讨论(0)
提交回复
热议问题