使用javascript设置表单值

时间:2016-02-11 17:54:21

标签: javascript html web

这是我正在使用的表单的剪辑。我使用javascript将美元和美分的值设置为2和00,但它不起作用。谁能纠正这个?谢谢

<select class="element select medium" id="element_8" name="element_8" onChange="update_txt()"> 
        <option value="" selected="selected"></option>
        <option value="First option" >First option</option>
        <option value="Second option" >Second option</option>
        <option value="Third option" >Third option</option>
</select>
        <input id="subtotal" name="subtotal" class="element text currency" size="10" value="" type="text" /> .      
        <label for="subtotal">Dollars</label>


        <input id="subtotalcents" name="subtotalcents" class="element text" size="2" maxlength="2" value="" type="text" />
        <label for="subtotalcents">Cents</label>

        <script>function update_txt(){
                price = document.getElementById('element_8').value;
                document.getElementById('element_7').value = price;
                document.getElementById('element_7').focus();
                document.getElementbyId('subtotal').value ='2';
                document.getElementbyId('subtotalcents').value ='00';
        }
        </script>

2 个答案:

答案 0 :(得分:1)

document.getElementById('subtotal').value ='2';是资本“B”

function update_txt(){
                price = document.getElementById('element_8').value;
                document.getElementById('element_7').value = price;
                document.getElementById('element_7').focus();
                document.getElementById('subtotal').value ='2';
                document.getElementById('subtotalcents').value ='00';
        }

P.S:您提供的代码中没有element_7,但希望它在您使用的实际代码中出现。

答案 1 :(得分:0)

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>


<script>function update_txt(){
  debugger
                price = document.getElementById('element_8').value;
                document.getElementById('element_7').value = price;
                document.getElementById('element_7').focus();
                document.getElementById('subtotal').value ='2';
                document.getElementById('subtotalcents').value ='00';
        }
        </script>

</head>
<body>   

<select class="element select medium" id="element_8" name="element_8" onChange="update_txt()"> 
        <option value="" selected="selected"></option>
        <option value="First option" >First option</option>
        <option value="Second option" >Second option</option>
        <option value="Third option" >Third option</option>
</select>
         <input id="element_7" name="element_7" class="element text currency" size="10" value="" type="text" /> .
        <input id="subtotal" name="subtotal" class="element text currency" size="10" value="" type="text" /> .      
        <label for="subtotal">Dollars</label>


        <input id="subtotalcents" name="subtotalcents" class="element text" size="2" maxlength="2" value="" type="text" />
        <label for="subtotalcents">Cents</label>

</body>
</html>