JavaScript 并获取单选按钮的值
JavaScript and getting the value of radio button
我尝试使用 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,其余的工作正常。我在谷歌上搜索了有关从单选按钮获取价值的信息,但没有发现与这种情况非常相关。
firebug 错误面板:
TypeError: document.getElementById(...) 为空
var coverkind = document.getElementById("cover").value;
如果需要,您可以使用输出标签或其他内容作为输出。 (例如 <p>
元素)。只需确保在 Javascipt 中为您想要访问的任何内容提供一个 'id' 值。
您需要创建一个新文件并将其命名为 'script.js'。 Google 如何将此脚本包含在您的 html 文档中。
您需要在脚本中使用的一些内容:
document.getElementById(str)
Returns an object representing an html
element with an id of 'str'
parseFloat(str)
Takes a string 'str' and return the float value
.value
This is a read/write property of an input text box object
that contains the text value
.checked
..and a property of an input radio button object.
当你碰壁时,请参考伟大的 google ;) 将所有这些放在一起,你应该在正确的轨道上。
这是您问题的解决方案。
首先给你的单选按钮一个相同的id。
勾选的属性会告诉你元素是否被选中:
<input type="radio" id="cover" name="cover" value="hard" checked="checked" />hard
<br />
<br>
<input type="radio" id="cover" name="cover" value="soft" />soft
在JavaScript函数中,可以得到。
var coverkind = null;
if (document.getElementById('cover').checked)
{
coverkind = document.getElementById('cover').value;
}
if (coverkind == 'soft') {
var SizePrice = (z * 800);
} else {
var SizePrice = ((y / 16) + 1) * 8 * z;
}
如果可以使用jQuery,就可以在一行中获取勾选单选按钮的值
var Val = $("input[name=cover]:checked").val();
我尝试使用 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,其余的工作正常。我在谷歌上搜索了有关从单选按钮获取价值的信息,但没有发现与这种情况非常相关。 firebug 错误面板:
TypeError: document.getElementById(...) 为空 var coverkind = document.getElementById("cover").value;
如果需要,您可以使用输出标签或其他内容作为输出。 (例如 <p>
元素)。只需确保在 Javascipt 中为您想要访问的任何内容提供一个 'id' 值。
您需要创建一个新文件并将其命名为 'script.js'。 Google 如何将此脚本包含在您的 html 文档中。
您需要在脚本中使用的一些内容:
document.getElementById(str)
Returns an object representing an html element with an id of 'str'
parseFloat(str)
Takes a string 'str' and return the float value
.value
This is a read/write property of an input text box object that contains the text value
.checked
..and a property of an input radio button object.
当你碰壁时,请参考伟大的 google ;) 将所有这些放在一起,你应该在正确的轨道上。
这是您问题的解决方案。
首先给你的单选按钮一个相同的id。 勾选的属性会告诉你元素是否被选中:
<input type="radio" id="cover" name="cover" value="hard" checked="checked" />hard
<br />
<br>
<input type="radio" id="cover" name="cover" value="soft" />soft
在JavaScript函数中,可以得到。
var coverkind = null;
if (document.getElementById('cover').checked)
{
coverkind = document.getElementById('cover').value;
}
if (coverkind == 'soft') {
var SizePrice = (z * 800);
} else {
var SizePrice = ((y / 16) + 1) * 8 * z;
}
如果可以使用jQuery,就可以在一行中获取勾选单选按钮的值
var Val = $("input[name=cover]:checked").val();