2016-03-15 103 views
1

我正在尝试创建某种桌面键盘(在我的文本字段中使用/写出吉利字母)。我遇到了问题,因为变量的赋值,我只能写第一个字母。使用按钮将文本写入输入框中

有人可以告诉我一个简单的方法来解决这个问题吗?

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
     <script language="javascript" type="text/javascript"> 
      function rukeyboard() { 
       var newtext = document.Keyboard.A.value; // <-- Problem is that newtext is only pointed to Keyboard.A but should be usable for 33 letters 
       document.Keyboard.outputtext.value += newtext; 
      } 
     </script> 
    </head> 
    <body> 
     <form name="Keyboard"> 
      <input type="button" name="A" value="A" onClick="rukeyboard();"> 
      <input type="button" name="B" value="B" onClick="rukeyboard();"> 
      <input name="outputtext"> 
     </form> 
    </body> 
</html> 

回答

0

试试这个

function rukeyboard(newtext) { 
 
    document.Keyboard.outputtext.value += newtext; 
 
}
<form name="Keyboard"> 
 
    <input type="button" name="A" value="A" onClick="rukeyboard(this.value);"> 
 
    <input type="button" name="B" value="B" onClick="rukeyboard(this.value);"> 
 
    <input name="outputtext" value=""> 
 
</form>

0

这应该可以解决你的目的:

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
     <script language="javascript" type="text/javascript"> 
      function rukeyboard(button) { 
       var newtext = button.value; // <-- Problem is that newtext is only pointed to Keyboard.A but should be usable for 33 letters 
       document.Keyboard.outputtext.value += newtext; 
      } 
     </script> 
    </head> 
    <body> 
     <form name="Keyboard"> 
      <input type="button" name="A" value="A" onClick="rukeyboard(this);">  <input type="button" name="B" value="B" onClick="rukeyboard(this);"> 
      <input name="outputtext"> 
     </form> 
    </body> 
</html> 

这里是一个小提琴:https://jsfiddle.net/wet32n06/

0

您也可以试试这个解决方案。

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
     <script language="javascript" type="text/javascript"> 
      function rukeyboard() { 
       // var newtext = document.Keyboard.A.value; // <-- Problem is that newtext is only pointed to Keyboard.A but should be usable for 33 letters 
       document.Keyboard.outputtext.value += document.activeElement.value; 
      } 
     </script> 
    </head> 
    <body> 
     <form name="Keyboard"> 
      <input type="button" name="A" value="A" onClick="rukeyboard();"> 
      <input type="button" name="B" value="B" onClick="rukeyboard();"> 
      <input name="outputtext"> 
     </form> 
    </body> 
</html> 

但个人而言,我更喜欢使用jquery关联函数,因为有很多输入标签,你必须手动调用每个输入标签。 所以一种好的方法是使用jquery.each来遍历所有输入,并将使用jquery on的函数绑定到输入。

0

下面是使用jQuery

HTML

<body> 
    <button value="A" name="A">A</button> 
    <button value="B" name="B">B</button> 
    <input id="outputtext" name="outputtext"> 
</body> 

JQuery的

<script> 
    $(document).on("click", "button", function() { 
      $("#outputtext").val($("#outputtext").val() + "" + this.value); 
    }); 
</script> 
的溶液