Javascript 使用标记保存要在算术中使用的整数
我只想有一个简单的程序,计算插入select标记中的预定义值,并将其乘以文本框的值,该值也将转换为整数,并在prod文本框中打印Javascript 使用标记保存要在算术中使用的整数,javascript,html,select,Javascript,Html,Select,我只想有一个简单的程序,计算插入select标记中的预定义值,并将其乘以文本框的值,该值也将转换为整数,并在prod文本框中打印 <script type="text/javascript"> //num2 is the id of my select tag var index = document.getElementById("num2").selectedIndex; //num1 is the id of my textbox //prod
<script type="text/javascript">
//num2 is the id of my select tag
var index = document.getElementById("num2").selectedIndex;
//num1 is the id of my textbox
//prod is the id of product textbox
function mul(){
var a = parseInt(document.getElementById("num1").value);
var b = document.getElementById("num2").value;
var c = document.getElementById("prod").value;
c.value=(a*b);
}
</script>
我快发疯了…@bloodyKnuckles是正确的,var b需要等于元素,而不是元素的value属性
必须获取该值并使用parseInt将其从字符串转换为整数:
parseInt(document.getElementById("first").value) + parseInt(document.getElementById("second").value);
这是:var c=document.getElementByIdprod;不是这个:var c=document.getElementByIdprod.value;。注意,我删除了value属性。嗯,答案仍然没有显示。也许有另一种解决这个问题的方法…:/哦,哇。非常感谢@joemfb,我是一名noob程序员。仍在学习基础知识。再次感谢你,朋友是一些小事让你着迷;祝你好运,继续前进!此外,如果答案令人满意,请单击它旁边的复选标记以接受它?谢谢
//num2 is the id of my select tag
var index = document.getElementById("num2").selectedIndex;
//num1 is the id of my textbox
//prod is the id of product textbox
function mul() {
debugger
var a = parseInt(document.getElementById("num1").value);
var b = document.getElementById("num2").value;
var c = document.getElementById("prod");
c.value = (a * b);
}