0

コンボボックスの選択が変わったとき、入力タグの値として特定の配列アイテムを設定したい。

    <%@ 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>

ローンの種類に応じて、配列からそれぞれの金利を選択し、この配列項目を name="rate" の入力タグの値として使用したいと考えています。

4

3 に答える 3

0

document.getElementById("elementid")名前をパラメータとして指定して使用することはできません。idパラメータは、名前ではなく要素でなければなりません。

this実際には、変更関数のパラメーターとして返されるため、document.getElementById を使用する必要はありません。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;">

そして、ここに完全なコードとデモがあります

<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>
于 2013-03-24T19:41:28.827 に答える
0

コンボ ボックスの値を取得するには、次の手順を実行する必要があります。

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

selあなたのコンボボックスです

于 2013-03-24T19:29:40.223 に答える
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];
}
于 2013-03-24T20:01:50.310 に答える