2016-03-15 399 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> 
的溶液
相關問題