在更改组合框时在输入框中插入一个数组项

时间:2013-03-24 19:20:18

标签: javascript

当combo1框的选择发生变化时,我希望将特定数组项设置为输入标记的值。

    <%@ page language="java" import="java.util.*;" pageEncoding="ISO-8859-1"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
        <head>
        </head>
    <script language="javascript">

    function check()
        {
            if(!validate())
                return false;
        }

    function validate()
        {
            if (document.form.amount.value <=0)
            {
                alert ("Please enter valid loan amount.");
                return false;
            }
            else
                return true;
        }

        var arr = new Array();
        arr[0] = new Array("-select-", "10.0","10.5", "12.0");

        function change(combo1) {
            document.getElementById("rate").style.visibility = "visible";
            var sel = document.getElementById("combo1");
        var val = document.getElementById("rate").value;
        for(var i = 0, j = sel.options.length; i < j; ++i) {
            if(sel.options[i].innerHTML == val) {
               sel.selectedIndex = i;
               break;
            }
        }
    }

}
    </script>
        <jsp:include page="include.jsp"></jsp:include>

        <body>
            <br>
            <form action="PaymentServlet.do" name="form" method="post"
                ONSUBMIT="return check()">
                <fieldset>
                    <legend>
                        <font style="font-family: sans-serif" size="4px" color="Megenta">Online Loan
                            Application Page</font>
                    </legend>
                    <br />

                    Loan Type
                    <select name="combo1" onchange="change(this);">
                        <option value="0">
                            -Select-
                        </option>
                        <option value="1">
                            Home Loan
                        </option>
                        <option value="2">
                            Education Loan
                        </option>
                        <option value="3">
                            Car Loan
                        </option>
                    </select>
                    <br />

                    Interest Rate(%)<input type="text" name="rate" style="visibility: hidden;">
                    <br />
                    Loan Amount
                    <input type="text" name="loanamount" />
                    <br />
                    <input type="submit" name="go" value="o Go o" />
                </fieldset>
            </form>
        </body>
    </html>

取决于贷款类型我想从数组中选择相应的利率,并希望将此数组项用作名称=“rate”的输入标记的值。

3 个答案:

答案 0 :(得分:0)

要获取组合框的值,您需要执行以下操作:

var val = sel.options[sel.selectedIndex].value;

sel是您的组合框

答案 1 :(得分:0)

您不能通过将名称作为参数来使用document.getElementById("elementid")。参数必须是id元素而不是名称。

实际上你不需要使用document.getElementById,因为你在更改函数中返回this作为参数。 this表示实际元素。

所以你的改变功能应该是这样的,

function change(combo1) {
       document.getElementById("rate").style.visibility = "visible";
       document.getElementById("rate").value = arr[combo1.selectedIndex];
}

您的数组定义也是错误的。正确的就在这里,

var arr = new Array();
arr[0] = new Array("-select-", "10.0","10.5", "12.0");

var arr = new Array("-select-", "10.0","10.5", "12.0");

最后不要忘记为费率输入添加id属性

<input type="text" id="rate" name="rate" style="visibility: hidden;">

这是完整的代码和DEMO

<script language="javascript">

function check()
    {
        if(!validate())
            return false;
    }

function validate()
    {
        if (document.form.amount.value <=0)
        {
            alert ("Please enter valid loan amount.");
            return false;
        }
        else
            return true;
    }

    var arr = new Array("-select-", "10.0","10.5", "12.0");

    function change(combo1) {
        document.getElementById("rate").style.visibility = "visible";
       document.getElementById("rate").value = arr[combo1.selectedIndex];
}

</script>
    <body>
        <br>
        <form action="PaymentServlet.do" name="form" method="post"
            ONSUBMIT="return check()">
            <fieldset>
                <legend>
                    <font style="font-family: sans-serif" size="4px" color="Megenta">Online Loan
                        Application Page</font>
                </legend>
                <br />

                Loan Type
                <select name="combo1" onchange="change(this);">
                    <option value="0">
                        -Select-
                    </option>
                    <option value="1">
                        Home Loan
                    </option>
                    <option value="2">
                        Education Loan
                    </option>
                    <option value="3">
                        Car Loan
                    </option>
                </select>
                <br />

                Interest Rate(%)<input type="text" id="rate" name="rate" style="visibility: hidden;">
                <br />
                Loan Amount
                <input type="text" name="loanamount" />
                <br />
                <input type="submit" name="go" value="o Go o" />
            </fieldset>
        </form>
    </body>

答案 2 :(得分:0)

如果您的数组定义类似于

arr[0] = new Array("-select-", "10.0","10.5", "12.0");

然后

function change(combo1) {
    document.getElementById("rate").style.visibility = "visible";
    var rate = document.getElementById("rate");
    rate.value = arr[0][combo1.selectedIndex];
}