我正在尝试创建某种桌面键盘(在我的文本字段中使用/编写抒情字母)。我遇到了问题,因为变量的赋值,我只能写第一个字母。
有人可以告诉我一个简单的解决办法吗?
<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 :(得分:0)
试试这个
function rukeyboard(newtext) {
document.Keyboard.outputtext.value += newtext;
}
&#13;
<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>
&#13;
答案 1 :(得分: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/
答案 2 :(得分: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将函数绑定到输入。
答案 3 :(得分: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>