显示/隐藏文本字段输入if 'Other'在多选择输入中选择
Javascript show/hide text field input if 'Other' selected in multiple select input
恐怕我被卡住了,我希望能得到一些社区智慧。
我正在构建一个HTML表单提交数据到SQL数据库。其中一个表单字段是一个多重选择输入字段,用户可以在其中选择要执行的操作类型。然后将该数组导出到SQL字段,作为逗号分隔的值。
我遇到的问题是,我还希望用户能够输入自定义操作类型,如果它不包括在提供的列表中。理想情况下,这将涉及在多重选择中选择"其他",并出现一个新的文本输入字段。
到目前为止,我只能使文本输入字段出现,如果用户选择了"其他",没有其他。如果用户选择了多个项目,其中一个是"其他",我可以让字段出现吗?
多谢
function showfield(episode_procedure){
if(episode_procedure=='Other')document.getElementById('otherprocedure').innerHTML='Other: <input type="text" name="episode_otherprocedure" style="width: 450px;"/>';
else document.getElementById('otherprocedure').innerHTML='';
}
<select multiple="multiple" data-placeholder="Click to select..." size="7" name="episode_procedure[]" style="width: 450px;" onchange="showfield(this.options[this.selectedIndex].value)"></p>
<option value="anterior resection">anterior resection</option>
<option value="appendicectomy">appendicectomy</option>
<option value="Other">Other</option></select>
<div id="otherprocedure"></div>
使用jQuery很容易获得多个select
标签中的所有值:
$(select).val()
现在您只需要检查'Other'
是否存在:
$(select).val().indexOf('Other') > -1
$('#s1').on('change', function() {
if ($(this).val().indexOf('Other') > -1) {
$('#otherprocedure').html('Other: <input type="text" name="episode_otherprocedure" style="width: 450px;"/>')
} else {
$('#otherprocedure').html('');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="s1" multiple="multiple" data-placeholder="Click to select..." size="7" name="episode_procedure[]" style="width: 450px;">
<option value="anterior resection">anterior resection</option>
<option value="appendicectomy">appendicectomy</option>
<option value="Other">Other</option></select>
<div id="otherprocedure"></div>
相关文章:
- 基于单选框更新页眉,选中并选择输入文本
- 我想在选择输入文件上显示图像
- 指令中选择输入的双向绑定不起作用
- 调整从选择输入中获取数据并将其放入 DIV 的 jQuery 代码
- JS根据类型和名称选择输入
- jQuery使用这个获取任何选择输入的name属性
- 无法检索从窗口.本地存储中选择输入值
- 使用Javascript与选择输入交互
- 在Meteor中加载带有采集数据的选择输入字段
- 使用 jquery 控制两个选择输入的组合值
- JavaScript 中的 if 语句,用于从选择输入中获取值
- 用文本输入填充选择输入
- 我无法使用jquery选择输入的值
- 如何在选择“输入颜色”后从中获取新的颜色值
- 如何添加单选按钮和复选框将选择输入到一起以形成JavaScript输出
- jQuery;Ajax-在页面加载时将项目加载到选择输入
- JQuery 验证器不适用于选择输入
- 如何使用 Reactjs 选择输入中的所有文本,当它聚焦时
- Angularjs:如何在页面加载时预先选择输入文本字段
- Jquery 不能在表单内选择输入元素,但在表单外部选择输入元素