简体   繁体   English

更改文本框Javascript的单个值

[英]Change a single value of a textbox Javascript

I want to change only one value of an input, example: in the input income "xy" I want to change is only "and" Suppose a new value is "p", and "x" leave. 我只想更改输入的一个值,例如:在输入收入“ xy”中,我要更改的只是“ and”,假设新值是“ p”,而“ x”离开。 The end result should be "xp". 最终结果应为“ xp”。

http://s2.subirimagenes.com/privadas/previo/thump_2265651input.png http://s2.subirimagenes.com/privadas/previo/thump_2265653input2.png http://s2.subirimagenes.com/privadas/previo/thump_2265651input.png http://s2.subirimagenes.com/privadas/previo/thump_2265653input2.png

<input type="text"id="txtChar1" onkeypress="isNumberKey();">

function isNumberKey()
{
  var text = document.getElementById(txtChar1).value;

  if (text[1] >= 0)
  {
    text[1].write("p");
    text[1].value ="p";
    text.charAt(1).value = "p";
    text[1].innerHTML = "p";
    text[1] = "p";
  }
}

Forget clarify the options that exist in this condition are those that probe and did not walk any. 忘记澄清在这种情况下存在的选项是那些探测并且没有行走的选项。

Strings are immutable. 字符串是不可变的。 Your description is unclear, but the best I can tell is that you want to reverse the order of the characters. 您的描述不清楚,但是我能告诉您的最好的一点是您想颠倒字符的顺序。

function isNumberKey()
{
  var el = document.getElementById("txtChar1");
  el.value = el.value.split("").reverse().join("");
}

The .split("") converts the string into an Array of its characters. .split("")将字符串转换为其字符数组。 Then .reverse() reverses the order of those Array members and .join("") joins the members back into a string. 然后, .reverse()反转这些Array成员的顺序,而.join("")将成员重新组合成字符串。


Another approach would be to pass this to the function, since that's the one getting updated. 另一种方法是this方法传递给函数,因为那是要更新的方法。

<input type="text"id="txtChar1" onkeypress="isNumberKey(this);">

function isNumberKey(el)
{
  el.value = el.value.split("").reverse().join("");
}

I won't go into detail about what was wrong with your code, because I think you were just throwing together a bunch of arbitrary bits of code in the hope that it would work. 我不会详细介绍您的代码出了什么问题,因为我认为您只是将一堆任意的代码组合在一起,希望它能起作用。 Needless to say, it's almost all wrong. 不用说,这几乎都是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM