gpt4 book ai didi

JavaScript 并获取单选按钮的值

转载 作者:行者123 更新时间:2023-11-27 23:54:03 24 4
gpt4 key购买 nike

我尝试使用 javascript 创建在线计算表单,除了单选按钮之外一切正常。

场景:

x(select list) =
item1 - value="11.2"
item2 - value="7.6"
item3 - value="7"

y=(input number)

z=(input number)

coverkind=(radio button)
if 1 selected >> coverkind = z*800
if 2 selected >> coverkind = ((y/16)+1)*8*z

totalprice= (x*y*z)+(1000*z)+coverkind

到目前为止我的工作:

<html>
<head>
<script type="text/javascript">
function getselectionPrice() {
var elt = document.getElementById("selectionone");
var selectionPrice = elt.options[elt.selectedIndex].value;
var y = document.getElementById("y").value;
var z = document.getElementById("z").value;
var ser = (selectionPrice * y * z);
var dz = z*1000;

var coverkind = document.getElementById("cover").value;
if (coverkind == 'soft') {
var SizePrice = (z * 800);
} else {
var SizePrice = ((y / 16) + 1) * 8 * z;
}

var finalPrice = ser + dz;
document.getElementById("totalPrice").value = finalPrice;
}
</script>
</head>
<body>
<div>

<form action="" id="calcform" onsubmit="return false;">
<div>
<div>
<fieldset>
<label>select</label>
<select id="selectionone" name='selectionone' onChange="getselectionPrice()">
<option value="11.2">1</option>
<option value="7.6">2</option>
<option value="7">3</option>
</select>
<br/>
<p>y
<input type="text" id="y" onchange="getselectionPrice()" />
</p>
<p>z
<input type="text" id="z" onchange="getselectionPrice()" />
</p>
<label>cover</label>
<input type="radio" name="cover" value="hard" />hard
<br />
<br>
<input type="radio" name="cover" value="soft" />soft
<br>
<br>
<br/>The new calculated price:
<INPUT type="text" id="totalPrice" Size=8>
</fieldset>
</div>
<input type='submit' id='submit' value='Submit' onclick="getselectionPrice()" />
</div>
</form>
</div>
</body>
</html>

如果我从 js 中删除 coverkind,其余的工作正常。我在谷歌上搜索了有关从单选按钮获取值(value)的信息,但没有发现与这种情况非常相关的内容。firebug 错误面板:

类型错误:document.getElementById(...) 为空var coverkind = document.getElementById("cover").value;

最佳答案

如果你会使用jQuery,你可以在一行中获取选中的单选按钮的值。

var Val = $("input[name=cover]:checked").val();

关于JavaScript 并获取单选按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32420248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com