我想创建一个简单的输入框,然后在您键入时计算80%但不使用表单

时间:2010-11-25 12:57:15

标签: javascript forms input

如果我不使用表单,我不知道如何从输入框中提取值...
所以我想复制以下内容,但不使用表单标签......

<form action="#" name="myconvert">
100% of value = 
<input type="text" name="percent100" 
onkeyup=" document.myconvert.percent80.value = Math.round((0.8 * document.myconvert.percent100.value)*100)/100" />
so 80% = <input type="text" name="percent80" disabled />

请帮忙

4 个答案:

答案 0 :(得分:2)

<input type="text" name="percent100"  
onkeyup=" document.getElementById('percent80').value = Math.round((0.8 * document.getElementById('percent100').value)*100)/100" /> 
so 80% = <input type="text" name="percent80" disabled /> 

答案 1 :(得分:1)

为输入提供ID,例如<input type="text" id="percent80" />,然后使用document.getElementById('percent80').value = ...

答案 2 :(得分:1)

使用jQuery:

<input type="text" id="txt" onKeyUp="show80()" />
<div id="show"></div>
<script type="text/javaScript">
function show80(){
        $('#show').html($('#txt').val()*0.8);
}
</script>

答案 3 :(得分:1)

您可以跳过表单标记,只需让Javascript以具有特定ID的输入标记为目标。

Type your value here <input id="source" type="text"><br/>
<br/>
<span id="result"></span>

<script type="text/javascript" language="javascript" src="jquery.min.js"></script>
<script type="text/javascript">
    $('#source').keyup(function(){
        $('#result').html( parseFloat($(this).val()) * 0.8 );
    });
</script>