首页 > 解决方案 > 条件表单疑难解答

问题描述

我是 Javascript 新手,正在尝试使用引导程序和 JQuery 构建条件表单。我非常感谢您的帮助,因为我大部分时间都在为此工作,但无济于事。

当具有名称的选择字段的值为or时,我试图显示div带有 id的(和后续字段) 。这是现场表格的链接physicianAppointmentTypeOrthopedicRheumatology

这是我的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 {}     
    }); 

});

标签: javascript

解决方案


您的选择器不正确:

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();

推荐阅读