Как добавить знак доллара в поле ввода, не влияя на значение ввода? - PullRequest
0 голосов
/ 01 июня 2018

Как добавить $ к следующему входу:

<td class="td10"><input type='text' class='form-control' id="boqRate" name="boq[boqRate]"></td>

Ответы [ 2 ]

0 голосов
/ 01 июня 2018

Попробуйте это.Я добавил элемент span с position:absolute.

.prefix {
  margin-right: -10px;
  position: absolute;
}

.my-input {
  padding-left: 5px;
}
<td class="td10"><span class="prefix">$</span><input class="my-input" type='text' class='form-control' id="boqRate" name="boq[boqRate]"></td>
0 голосов
/ 01 июня 2018

Это должно работать:

let input = document.getElementById("name");

input.addEventListener("keydown", () => {
  let inputValue = input.value;
  if (inputValue) {
    if (inputValue.charAt(0) === "$") {
      inputValue = inputValue.substring(1);
    }
  }
  let newValue = `$${inputValue}`;
  input.value = newValue;
});
<input id="name" />

Надеюсь, это поможет!

...