簡體   English   中英

根據下拉菜單中的選項在文本框中設置整數值

[英]setting an integer value in a textbox according to the option selected from a dropdown

我想在文本框中設置一個整數值,具體取決於從下拉菜單中選擇的選項。 dropDown中的每個選項都會在文本框中設置不同的值。 這是我的代碼。 等待有用的答案。

<html>

    <head>

        <script>

    function myfunction(val)
    {
            var x=document.getElementById('ops');

            var y=document.getElementById('text');

            var z=document.getElementById('lb');
            //if(val='pepsi')

                z.style.display = "block";

                y.style.display = "Inline";

                }

        function setval(data)
        {


            var b=document.getElementById('ops');
            var a=document.getElementById('txtprice');

            if (data.value='pepsi')
                {
                var c='50';
                a.value=parseInt(c);    
            }
            else if(data.value='fanta') 
                {

                a.value=parseInt(12);

                }
        // other options 
        }


    function myfun2()
            {
            var c=document.getElementById("lb1");

            var b=document.getElementById("btn1");

            var a=document.getElementById('dte');

            b.style.display="block";

            c.style.display="block";

            a.style.display="block";
            }

    function validateform()
        {

        var n = document.getElementById("text");

    if(n.value==null || n.value==" ")
        {
        alert("please enter any val.");

            return false;
        }
    else
    {

        a = isNaN(n.value);
    //return ( ! isNaN(n.value));

    if(a==true)
    {
        alert ("Characters are not allowed :( ");

        return false;
    }
    else

        return true;    
    }
        }

            </script>


        </head>

<body>

 <form name='mform' action="process.php"  onSubmit="return validateform()">

        <select id="ops" onChange="setval(this)">

                        <option>PIck item </option>
                        <option value="pepsi">pepsi</option>
                        <option value="coke">coke</option>
                        <option value="fanta">Fanta</option>

        </select><br><br>


    <label id="lb" style="display:none" >Qty</label>

    <input type="text" name='qtytext' id="text" style="display:none" onKeyUp="return validateform()" on onSelect="return validateform()" onFocus="myfun2()" required/ >


    <label id="lb1" style="display:none" >.....Before proceeding, Make sure that form is properly filled.....</label>

    <input type="submit" name'btn' id='btn1' value="Submit" style='display:none' /> <br>

    <input type="date" name="dte" id='dte' style="display:none" onKeyUp="return validateform()"  onFocus="myfun2()">

    <input type="text" name'txt' id='txtprice' /> <br>

</form>

        </body>

                </html>

您的文本框輸入中存在語法錯誤。 應該是(您缺少名稱上的=符號):

<input type="text" name='txt' id='txtprice' />

另外,您應該將字符串而不是int傳遞給文本框。 因此,例如,更改此:

a.value=parseInt(c);

對此:

a.value = '50';

UPDATE(也更新了小提琴鏈接):

您還將在條件中分配值,而不是評估它們。 例如:

if (data.value='pepsi')

應該:

if (data.value=='pepsi')

注意:我只評估了可解​​決OP提出的特定問題的代碼部分,並未在代碼的其他區域中查找問題。

JSFiddle演示

這是一個有關如何實現此目標的簡單演示 您可能需要對其進行一些更改以適合您的腳本,但這將向您展示如何從下拉菜單更改文本框中的值的基礎知識。

基本上:

的HTML

<select id="sel">
   <option value="50">Pepsi</option>
   <option value="100">Fanta</option>
   <option value="150">Cola</option>
</select>    
<input type="text" id="txt">

jQuery的

$('#sel').on('change', function() {
   $('#txt').val($(this).val());
});

如果您更喜歡使用普通Javascript而非jQuery,只需將上面的jQuery腳本替換為:

JS

document.getElementById("sel").onchange = function(){
   document.getElementById("txt").value = this.value;
}

演示版

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM