【问题标题】:How can I use function to place inputs on this code? I have no clue because I am a beginner如何使用函数在此代码上放置输入?我不知道,因为我是初学者
【发布时间】:2014-03-16 04:51:34
【问题描述】:

当用户定义姓名和 ID、工资、活动、背景和付款方式时,我需要帮助放置输入,我已经处理了此代码并且一切正常,我需要的只是输入,但我遇到了一些麻烦这个过程的逻辑,我只是​​一个初学者。提前致谢。

<head>
    <meta charset="utf-8">
    <title>INSURANCE</title>
</head>
<body>
    <script>
        var d=document,
            nameId=prompt('Enter Name and ID'),
            salary=prompt('Enter Salary'),
            activity=Number(prompt('\nDedicated: \n1.Administration and education \n2.Industry \n3.Transportation\n4.Construction')),
            background=Number(prompt('\nBackground: \n1.0-3 \n2.4-10 \n3.10+')),
            tipoPago=Number(prompt('\nPayment way: \n1.Monthly \n2.Quarterly \n3.Biannual \n4.Anual')),
            totalhis = 0, totalPay=0, total=0, prime=0; //NOTICE!!! 4 vars single line.
                //switch for activity
            var admiNedu=salary*0.03,
                industry=salary*0.06,
                transport=salary*0.10,
                construction=salary*0.15;

            switch (activity){
                case 1: prime=admiNedu;
                        break;
                case 2: prime=industry;
                        break;
                case 3: prime=transport;
                        break;
                case 4: prime=construction;
                        break;
                    }
                    //switch for background
            var months3=0,
                months4to10=prime*0.02,
                plus10=prime*0.05;

            switch (background){
                case 1: totalhis=0;
                        break;
                case 2: totalhis=months4to10;
                        break;
                case 3: totalhis=plus10;
                        break;
                    }
                    //switch for tipoPago
            var monthlyPay=prime*0.06,
                trimestPay=prime*0.05,
                semestPay=prime*0.04,
                anualPay=0;

            switch (tipoPago){
                case 1: totalPay=monthlyPay;
                    break;
                case 2: totalPay=trimestPay;
                    break;
                case 3: totalPay=semestPay;
                    break;
                case 4: totalPay=anualPay;
                    break;
            }
            total=prime+totalhis+totalPay;
            d.writeln('Name and ID ' + nameId)
            d.writeln('Salary ' + salary)
            d.writeln(total)
    </script>
</body>

【问题讨论】:

  • 放置输入是什么意思?
  • @MatúšDúbrava 当您在 input='text' id='bla bla bla' 上输入信息时,我不知道如何使它工作。我需要的是用程序要求的信息填写表格的输入。因此,不要放置可怕的提示,而是放置空白,这样用户只需输入信息而不看提示。
  • 我还是不明白。您是在问如何从输入框中获取值(由用户输入)?
  • @MatúšDúbrava 看看这个例子,对不起我的英语,但我不是母语人士&lt;body&gt;&lt;button type='button' onclick='inicio()'&gt;Try it&lt;/button&gt; &lt;input type='text' id='texto' value='hola'&gt;&lt;/input&gt; &lt;script&gt; function inicio(){ var resultado=suma(12,3,4); document.getElementById('texto').setAttribute('value', resultado);} function suma(n1,n2,n3){ var total=n1+n2+n3; return total; } &lt;/script&gt; &lt;/body&gt;

标签: javascript function switch-statement


【解决方案1】:

您可以像这样更改输入文本字段的值(使用 jquery):

var value = "something"; // value to be displayed

$("#text-input").val(value);  // where text-input is id of the input field

小提琴:http://jsfiddle.net/CBSeZ/4/

使用纯 JavaScript:

var value = "something";

var elem = document.getElementById("text-input");
elem.value = value;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    相关资源
    最近更新 更多