从选择框中获取选定值无效
Getting a selected value from select box doesnt work
我制作了一个函数,在点击时使用另一个选择框中的值填充一个选择盒,它对系统中的大多数选择框都很有效,但我现在很难处理这个问题lol:
用于测试所选值并在发现时发出警报的功能:
function updateSelectBox(parent, child){
for(i = 0; i < document.getElementById(parent).length; i++){
if(document.getElementById(parent).options[i].selected){
alert(document.getElementById(parent).options[i].value);
}
}
选择此选项时不会发出警报:
<input type="hidden" name="data[Series][3][Profession][Profession]" value="" id="Professions3_">
<select name="data[Series][3][Profession][Profession][]" multiple="multiple" id="Professions3" onchange="updateSelectBox("Professions3", "Specialisms3");">
<option value="24">Scientist</option>
选择时会发出警报:
<input type="hidden" name="data[Series][4][Profession][Profession]" value="" id="Professions4_">
<select name="data[Series][4][Profession][Profession][]" multiple="multiple" id="Professions4" onchange="updateSelectBox("Professions4", "Specialisms4");">
<option value="24">Scientist</option>
这是来自不同选择框的完整html输出,也是WORKING
<div class="input select"><label for="Zones">Zones</label><input type="hidden" name="data[Series][0][Zone][Zone]" value="" id="Zones_">
<select name="data[Series][0][Zone][Zone][]" multiple="multiple" id="Zones" onchange="updateSelectBox("Zones", "Countries");">
<option value="4">Europe</option>
</select>
</div>
这不起作用。即使它在你的页面上有效,你帖子中的代码也无法工作:你的JavaScript缺少右括号,你使用的是双引号中有双引号的HTLM,没有人能够运行它。因此,让我们退一步,将其改写为肯定有效的东西:
HTML
<select onchange="updateSelectBox(this);">
<option value="0">Scientist 1</option>
<option value="12">Scientist 2</option>
<option value="24">Scientist 3</option>
</select>
JS-
function updateSelectBox(selectBox) {
var sid = selectBox.selectedIndex;
var selectedOption = selectBox.children[sid];
console.log(selectedOption);
}
现在,我们至少有了一些我们知道有效的东西,使用最少的代码,我们可以重新构建。我建议使用它来构建您现在拥有的HTML和函数。例如,您肯定需要停止使用这么多字符串和查找。特别是相信字符串没有拼写错误会毁了你的一天(你有一个名为"专业3_"的输入和一个称为"专业3"的选择……这只是在找麻烦)
相关文章:
- 获取所选选项的Javascript在Webkit/FF中有效,但在IE中无效
- TypeError: 使用 Jquery JSON 获取数据时无效的 'in' 操作数 obj
- 无效的“in”操作数 obj JQUERY - 尝试从 JSON 获取数据时出现类型错误
- 相位器.js获取给定的无效相位器状态对象
- PHP 和 RegEx:在 XML 标记之间获取原始数据,即使整个 XML 似乎无效
- Getting TypeError:无效'在'使用ajax获取数据时的操作数obj
- 如何获取XML文件中节点的值.代码无效
- ReactFire-获取'对象作为React子对象无效'将Firebase绑定到React状态时
- 获取输入框的值无效
- 从输入类型number中获取无效值
- 获取'无效的返回语句'我也不知道为什么
- 获取“websocket连接无效”;使用套接字出错.ec2实例上的IO
- 如何获取无效输入的内容长度
- 通过id获取元素无效
- D3指令-获取路径无效值的错误
- 使用jQuery在大括号中获取无效的HTML代码
- 带有浏览器.js的 Babel,获取未捕获的语法错误:正则表达式无效
- 获取访问令牌时,签名LinkedIn Oauth无效
- 从选择框中获取选定值无效
- Jquery ajax发送数据并获取:SQLSTATE[HY093]:无效的参数编号