javascript - 条件表单疑难解答
问题描述
我是 Javascript 新手,正在尝试使用引导程序和 JQuery 构建条件表单。我非常感谢您的帮助,因为我大部分时间都在为此工作,但无济于事。
当具有名称的选择字段的值为or时,我试图显示div
带有 id的(和后续字段) 。这是现场表格的链接。 physician
AppointmentType
Orthopedic
Rheumatology
这是我的javascript:
$( document ).ready(function() { //wait until body loads
//Inputs that determine what fields to show
var appttype = $('#secureform input:select[name=AppointmentType]');
var physician = document.getElementById("physician");
appttype.change(function(){ //when the Appointment Type changes
var value=this.value;
physician.addClass('hidden'); //hide everything and reveal as needed
if (value === 'Orthopedic' || value === 'Rheumatology'){
physician.removeClass('hidden'); //show doctors
}
else {}
});
});
解决方案
您的选择器不正确:
var appttype = $('#secureform input:select[name=AppointmentType]');
// this should be
var appttype = $('#secureform select[name=AppointmentType]');
此外,您正在将 jquery 与 vanilla JS 混合。你在这里使用香草js
var physician = document.getElementById("physician");
Physician 现在是 dom 对象而不是 jquery 对象。你应该改用这个:
var physician = $("#physician");
此外,您应该更换
var value=this.value;
有了这个
var value= $(this).val();
推荐阅读
- facebook-opengraph - Facebook Share 未从 Gatsby 网站获取 OpenGraph 元标记
- javascript - Node 中的 JavaScript:console.timeEnd() 的意外行为
- git - Jenkins项目之间的差异
- vb.net - 基于 GridView 创建 Panels
- javascript - 自动一个接一个地提交多个表单
- h2 - H2 插入数据失败
- slack - 显示员工 ID 而不是员工姓名的桌面 Slack 应用程序
- node.js - 如何在 mongoDB 中动态更新/设置字段?
- python - 单行 if 语句的覆盖范围
- python - 破解后如何重新启动相同的功能