如何从Javascript获取值来输入文本表单?

时间:2013-03-13 01:17:20

标签: javascript forms select textinput

我有一个选择的形式与普通,家庭,豪华,行政等各种酒店房间有相应的价格让我们说在普通的房间我会设置300美元的价格。

在选择表单的右侧,每次我选择让我们说普通房间的右侧输入文本字段,它将显示价格300美元。所以基本上我使用代码var ordinary=document.getElementByID("ord").value的java脚本,以便我可以设置价格var ordinary= 3500。接下来是每次在选择表格中选择普通房间时,从输入文本字段中显示变量普通值,但是我该如何做呢?

这是代码

<script language="javascript" type="text/javascript">
    var ordinary= document.getElementById("ord").value;
    var ordinary= 5300;

</script>

<form action="insert.php" method="post">
    <table align="center"><tr><td>
            <font face="Arial, Helvetica, sans-serif">RESERVATIONS</font> 
        </td></tr><hr color="#00CC99"/> <br/>
    </table>
    <table border="0" align="center">
        <tr>    
            <th align="justify"> Name: &nbsp </th> 
            <td><input type="text" name="name"></td> 
        </tr>
        <tr>
            <th align="justify"> Contact Number: &nbsp; </th> 
            <td><input type="text" name="contact"></td> 
        </tr>
        <tr>
            <th  align="justify">Room Type: &nbsp;</th>
        <td>
            <select name="roomType">
                <option value="---">---------------------------</option>
                <option value="Ordinary " id="ord">Ordinary</option>
                <option value="Family ">Family</option>
                <option value="Superior ">Superior</option>
                <option value="Deluxe ">Deluxe</option>
                <option value="Corner Suite King">Corner Suite King</option>
                <option value="Executive ">Executive</option>
                <option value="Executive Suite">Executive Suite</option>
                <option value="Grand Executive">Grand Executive</option>
                <option value="Presidential ">Presidential</option>
            </select>
        </td>
            <td>

            </td>
        </tr>
        <tr>
            <th align="justify">AddOn Services: &nbsp;</th>
        </tr>
    </table>
</form>

1 个答案:

答案 0 :(得分:3)

select id roomType,然后使用此

var e = document.getElementById("roomType");
var desiredValue = e.options[e.selectedIndex].value;

选项的值应该是脚本所需的数据。你应该调用select元素的selectedIndex而不是options,否则这将是我们所有人必须输入的很多代码......没有乐趣

    <select name="roomType" id="roomType">
        <option value="---">---------------------------</option>
        <option value="5300">Ordinary</option>
        <option value="SomePrice">Family</option>
        <option value="SomePrice">Superior</option>
        <option value="SomePrice">Deluxe</option>
        <option value="SomePrice">Corner Suite King</option>
        <option value="SomePrice">Executive</option>
        <option value="SomePrice">Executive Suite</option>
        <option value="SomePrice">Grand Executive</option>
        <option value="SomePrice">Presidential</option>
    </select>

将SomePrice更改为您想要的值。