“replace is not a function”

前端 未结 3 325
梦毁少年i
梦毁少年i 2021-01-20 22:47

I\'m trying to remove the comma from a number.

var thisbill_str = \"\"; 
thisbill = $(\'#linebill_\' + z).val(); 
if (isNaN(thisbill) ) { thisbill = 0.00; } 
thi         


        
相关标签:
3条回答
  • 2021-01-20 23:32

    TypeError: thisbill_str.replace is not a function

    thisbill_str can't be coerced in to a String object, therefore it doesn't have the replace method.

    Is the value of thisbill_str null?

    0 讨论(0)
  • 2021-01-20 23:37
    thisbill_str = thisbill; 
    

    You should be casting to a string here. thisbill_str is still a number, so it doesn't have a replace method.

    thisbill_str = thisbill + '';
    
    0 讨论(0)
  • 2021-01-20 23:46

    It is most likely caused by thisbill_str being something other than a String. Maybe you have some other code somewhere that automatically converts thisbill_str to a Number?

    You can convert back to a string using String(thisbill_str).

    The complete code would be:

    thisbill = String(thisbill_str).replace(",", "")
    
    0 讨论(0)
提交回复
热议问题