Fix a character to text input box using javascript/jquery

后端 未结 5 1846
离开以前
离开以前 2020-12-08 11:24

I am looking for a way to \'fix\' a dollar symbol $ to a text input box eg but make it not possible for the default va

5条回答
  •  渐次进展
    2020-12-08 11:48

    I did it like this:

    $('#price').keypress(function(event) {
    var code = (event.keyCode ? event.keyCode : event.which);
    if ($(this).val().indexOf('$') === -1){
        document.getElementById("price").value = "$" + $(this).val();
      } else {
        document.getElementById("price").value = $(this).val();
      }  
    });
    

    with .

    User can remove the dollar sign manually though if he/she wants to.

提交回复
热议问题