问题描述
我无法获取表单元素的值,即使从其他类似的问题中应该可以这样做。
仅供参考-我有一个函数可以将2个id(#SELECTED_SIGN&;#SELECTED_SIGN_FORM)写入一篇文章和一个嵌套在文章中的表单-这纯粹是为了样式/标识目的。
以下是根据分配的ID获取元素值失败的函数:
var sSize_prices= new Array();
sSize_prices["45"]=45; //per sq ft
sSize_prices["60"]=65; //per sq ft
function getSizePrice() {
var getSizePrice=0;
var theForm = document.forms["selected_sign_form"];
var selectedSize = theForm.elements["os0"];
alert(selectedSize.value);
getSizePrice = sSize_prices[selectedSize.value];
return getSizePrice;
}
以下是包含ID的文章和表单的HTML标记:
<section class="sign_type">
<h3>Selection</h3>
<article class="signs" onclick="calculateTotal()" id="selected_sign">
<figure class="sample">
<a href="/cat/s1b.png" rel="lightbox" title="Sign preview">
<img class="sign_preview" src="/cat/s1.png" alt="Sign preview" title="Sign preview" />
</a>
</figure>
<form action="" class="options" onsubmit="return false;" id="selected_sign_form">
<div class="sign_option1"><label class="on0">Size:</label>
<select name="Size" class="os0" onclick="calculateTotal()">
<option value="45">45cm sq</option>
<option value="60">60cm sq</option>
</select>
</div>
<div class="sign_option2"><label class="on1">Qty:</label>
<input name="Quantity" class="os1" value="1" onkeyup="calculateTotal()" />
</div>
<div class="sign_option3"><label class="on2">Fine:</label>
<input name="Custom" class="os2" value="" />
</div>
</form>
<div class="price"></div>
</article>
</section>
推荐答案
您的代码很好,只需要稍微修改一下。
以下行在您的表单中查找属性"name"等于"os0"的元素:
theForm.elements["os0"];
如果您将"选择"标记的名称设置为"os0",它将起作用。
例如
<select name="os0" class="os0" onclick="calculateTotal()">