【发布时间】:2014-11-05 06:04:55
【问题描述】:
我无法获取表单元素的值,即使从其他类似的问题中这应该可以工作。
仅供参考 - 我有一个函数可以将 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>
【问题讨论】:
-
我认为您不能像这样分配数组值:
sSize_prices["45"]=45;。在 JavaScript 中,您只能按数字访问数组索引。如果你想要现在的方式,你需要使用Object -
那纯粹是对照数组检查
值以找到相应的“价格”。它工作得很好,特别是对于每个选项具有不同定价的大型选项数组。
标签: jquery forms select element