如何将字符串放在文本框中?

时间:2015-12-24 23:19:35

标签: javascript html

所以我正在创建这个函数,但我不知道如何将数据路由到文本框。

<!DOCTYPE html>
<title>Percent of Letters</title>
<textbox id = "a"></textbox>
<script>
    document.getElementById("a").value += "A:" aPercent "%";
    var str = "abcdefghijklmnopqrstuvxyz";
    var a = (str.split('a').length);
    var b = (str.split('b').length);
    var c = (str.split('c').length);
    var d = (str.split('d').length);
    var e = (str.split('e').length);
    var f = (str.split('f').length);
    var g = (str.split('g').length);
    var h = (str.split('h').length);
    var i = (str.split('i').length);
    var j = (str.split('j').length);
    var k = (str.split('k').length);
    var l = (str.split('l').length);
    var m = (str.split('m').length);
    var n = (str.split('n').length);
    var o = (str.split('o').length);
    var p = (str.split('p').length);
    var q = (str.split('q').length);
    var r = (str.split('r').length);
    var s = (str.split('s').length);
    var t = (str.split('t').length);
    var u = (str.split('u').length);
    var v = (str.split('v').length);
    var x = (str.split('x').length);
    var y = (str.split('y').length);
    var z = (str.split('z').length); 
    var aPercent = (a / str.length * 100);
    var bPercent = (b / str.length * 100);
    var cPercent = (c / str.length * 100);
    var dPercent = (d / str.length * 100);
    var ePercent = (e / str.length * 100);
    var fPercent = (f / str.length * 100);
    var gPercent = (g / str.length * 100);
    var hPercent = (h / str.length * 100);
    var iPercent = (i / str.length * 100);
    var jPercent = (j / str.length * 100);
    var kPercent = (k / str.length * 100);
    var lPercent = (l / str.length * 100);
    var mPercent = (m / str.length * 100);
    var nPercent = (n / str.length * 100);
    var oPercent = (o / str.length * 100);
    var pPercent = (p / str.length * 100);
    var qPercent = (q / str.length * 100);
    var rPercent = (r / str.length * 100);
    var sPercent = (s / str.length * 100);
    var tPercent = (t / str.length * 100);
    var uPercent = (u / str.length * 100);
    var vPercent = (v / str.length * 100);
    var xPercent = (x / str.length * 100);
    var yPercent = (y / str.length * 100);
    var zPercent = (z / str.length * 100);
</script>

我希望它读作(字母):x%of string

2 个答案:

答案 0 :(得分:1)

你忘记了几件事。使用+运算符连接字符串和数字。只需更改此行:

document.getElementById("a").value += "A:" aPercent "%";

document.getElementById("a").value += "A:" + aPercent + "%";

在计算完所有百分比后,将其推到代码的底部。

此外,textbox不是有效的HTML元素。请改用textareaPlunker

另外,我猜你是javascript的新手,有更好的方法来实现这样的逻辑。您不必定义任何数量的变量。当您了解它们时,您可以使用array

答案 1 :(得分:0)

您可以使用其他方法,请先包含任何jquery min js

  var a=5;
 $('.yourinputBoxclassname').val(a);
  var a='your string';
 $('.yourinputBoxclassname').val(a);