我使用inArray
来检查从控件中选择的选项是否存在于多个阵列中的一个阵列中(我然后使用它来自定义和显示某些文本)。
我的代码位于http://codepen.io/marklsanders/pen/rWGgPE
我使用一系列if()
和else if(0)
通过inArray()检查所选值是否存在,并将结果与-1
进行比较。< / p>
else if (jQuery.inArray(course, set_2) != -1) {text = text_2;}
在每种情况下,代码都会落到最终的else
- 值未找到。
我确定某处有一个愚蠢的错误,但我无法看到它。
感谢
答案 0 :(得分:1)
course
需要是一个数字。请记住,输入元素中的任何文本都是字符串,即使文本是1,2,3等等。因此,如果您需要输入中的数字,请使用{{1将数据转换为数字},Number(data)
或parseInt(data, 10)
。此外,如果您的输入为parseFloat(data)
,则在将其转换为数字之前,它仍然是一个字符串。
type="number"
答案 1 :(得分:1)
课程变量必须具有相同数据类型的数组项。将您的course
变量转换为数字。
更改
var course = $('#choose-a-course').val();
到此
var course = Number($('#choose-a-course').val());
答案 2 :(得分:0)
你的课程编号是一个字符串,将其转换为数字
$(document).ready(function() {
var set_1 = [2, 3, 4, 7, 11, 12, 13, 14, 15, 16, 17, 18, 19, 29, 30, 31, 36, 37, 38, 39, 43, 44, 45, 55, 58, 59, 61, 62, 63, 64, 67, 68, 69, 76, 77, 79, 85, 86, 90, 91, 92, 93, 95, 96, 97, 100, 112, 113, 116, 117, 118, 119, 122, 133, 139, 147, 148, 149];
var set_2 = [46, 48, 49, 50, 70, 81, 128, 129, 130, 131, 132, 136, 137, 138, 141, 142, 143];
var set_3 = [1, 22, 23, 24, 25, 26, 27, 28, 66, 80, 83, 84, 94];
var set_4 = [8, 9, 10];
var set_5 = [41, 42, 134, 135];
var set_6 = [5, 6, 33, 34, 54, 60, 74, 75, 87, 88, 89, 99, 123, 144];
var set_7 = [35, 56, 57, 124];
var set_8 = [65, 101, 102, 107, 108, 109];
var set_9 = [20, 21, 32, 40, 51, 52, 53, 71, 72, 73, 78, 82, 98, 103, 104, 105, 106, 111, 114, 115, 120, 121, 125, 126, 127, 140, 145, 146];
//console.log ("hello");
var text = "";
var heading = "<h2>Applicant Visit Days</h2><p>Applicant Visit Days are available for the course you chose on the following dates:</p>";
$('.hide-me').hide();
text_1 = '<ul><li>8 Feb</li><li>25 Feb</li><li>22 April</li></ul>';
text_2 = '<ul><li>15 Feb</li><li>22 March</li><li>22 April</li></ul>';
text_3 = '<ul><li>22 Feb</li><li>25 Feb</li><li>22 April</li></ul>';
text_4 = '<ul><li>8 Feb</li><li>25 Feb</li></ul>';
text_5 = '<ul><li>22 Feb</li><li>22 April</li></ul>';
text_6 = '<ul><li>15 March/li><li>22 April</li></ul>';
text_7 = '<ul><li>25 Feb</li><li>15 March</li><li>22 April</li></ul>';
text_8 = '<ul><li>22 March</li><li>22 April</li></ul>';
text_9 = '<ul><li>22 April</li></ul>';
$('#choose-a-course').change(function() {
//console.log("changed");
var course = parseInt($('#choose-a-course').val(), 10);
console.log("Value changed to " + course);
if (jQuery.inArray(course, set_1) != -1) {
alert(text);
text = text_1;
} else if (jQuery.inArray(course, set_2) != -1) {
text = text_2;
} else if (jQuery.inArray(course, set_3) != -1) {
text = text_2;
} else if (jQuery.inArray(course, set_4) != -1) {
text = text_2;
} else if (jQuery.inArray(course, set_5) != -1) {
text = text_2;
} else if (jQuery.inArray(course, set_6) != -1) {
text = text_2;
} else if (jQuery.inArray(course, set_7) != -1) {
text = text_2;
} else if (jQuery.inArray(course, set_8) != -1) {
text = text_2;
} else if (jQuery.inArray(course, set_9) != -1) {
text = text_2;
} else {
text = "couldn't match the value";
}
//console.log (text);
$('.hide-me').html(heading + text);
$('.hide-me').show("slow");
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-12">
<h2>Choose a course</h2>
<select id="choose-a-course">
<option selected disabled>Choose a course:</option>
<option value="1">Accounting and Finance (BSc)</option>
<option value="2">Aeronautical Engineering (BEng/MEng)</option>
<option value="3">Aeronautical Engineering with Foundation Year (BEng)</option>
<option value="4">Aircraft Engineering with Pilot Studies (BEng/MEng)</option>
<option value="5">Animation (BA)</option>
<option value="6">Animation with Industry Placement (BA)</option>
<option value="7">Archaeology and Geography with Professional Practice (BSc)</option>
<option value="8">Architectural Design and Technology (Accelerated Degree) (BSc)</option>
<option value="9">Architectural Design and Technology (BSc)</option>
<option value="10">Architecture (BSc)</option>
<option value="11">Audio Acoustics (BEng)</option>
<option value="12">Audio Acoustics with Foundation Year (BEng)</option>
<option value="13">Aviation Technology with Pilot Studies (BSc)</option>
<option value="14">Biochemistry (BSc)</option>
<option value="15">Biochemistry with Studies in USA (BSc)</option>
<option value="16">Biology (BSc)</option>
<option value="17">Biology Foundation Year</option>
<option value="18">Biology with Studies in USA (BSc)</option>
<option value="19">Biomedical Science (BSc)</option>
<option value="20">Building Surveying (Accelerated Degree) (BSc)</option>
<option value="21">Building Surveying (BSc)</option>
<option value="22">Business and Economics (BSc)</option>
<option value="23">Business and Financial Management (BSc)</option>
<option value="24">Business and Management (BSc)</option>
<option value="25">Business and Tourism Management (BSc)</option>
<option value="26">Business Information Technology (BSc)</option>
<option value="27">Business Management with Law (BSc)</option>
<option value="28">Business Management with Sport (BSc)</option>
<option value="29">Civil and Architectural Engineering (BEng/MEng)</option>
<option value="30">Civil Engineering (BSc/BEng/MEng)</option>
<option value="31">Civil Engineering with Foundation Year (BEng)</option>
<option value="32">Comedy Writing and Performance (BA)</option>
<option value="33">Computer and Video Games (BSc)</option>
<option value="34">Computer and Video Games with Industy Placement (BSc)</option>
<option value="35">Computer Networks (BSc)</option>
<option value="36">Computer Science (BSc)</option>
<option value="37">Computer Science and Web Development(BSc)</option>
<option value="38">Computer Science with Cyber Security (BSc)</option>
<option value="39">Computer Science with Data Analytics (BSc)</option>
<option value="40">Construction (HNC)</option>
<option value="41">Construction Project Management (Accelerated Degree) (BSc)</option>
<option value="42">Construction Project Management (BSc)</option>
<option value="43">Contemporary History and Politics (BA)</option>
<option value="44">Contemporary Military and International History (BA)</option>
<option value="45">Corporate Law (LLB)</option>
<option value="46">Counselling and Psychotherapy: Professional Practice (BSc)</option>
<option value="47">Course title</option>
<option value="48">Criminology (BSc)</option>
<option value="49">Criminology and Sociology (BSc)</option>
<option value="50">Criminology with Counselling (BSc)</option>
<option value="51">Dance (BA)</option>
<option value="52">Design (BA)</option>
<option value="53">Diagnostic Radiography (BSc)</option>
<option value="54">Digital Media (BA)</option>
<option value="55">Drama and Creative Writing (BA)</option>
<option value="56">Electronic Engineering (BEng)</option>
<option value="57">Electronic Engineering with Foundation Year(BEng)</option>
<option value="58">English and Creative Writing (BA)</option>
<option value="59">English and Drama (BA)</option>
<option value="60">English and Film (BA)</option>
<option value="61">English Language (BA)</option>
<option value="62">English Language and Creative Writing (BA)</option>
<option value="63">English Literature (BA)</option>
<option value="64">English Literature with English Language (BA)</option>
<option value="65">Enhancing Professional Health Care Practice (BSc)</option>
<option value="66">Enterprise, Leadership and Management (BSc)</option>
<option value="67">Environmental Health (BSc)</option>
<option value="68">Environmental Management (BSc)</option>
<option value="69">Environmental Management Foundation Year</option>
<option value="70">Exercise, Physical Activity and Health (BSc)</option>
<option value="71">Fashion Design (BA)</option>
<option value="72">Fashion Image Making and Styling (BA)</option>
<option value="73">Film and TV Set Design (BA)</option>
<option value="74">Film Production (BA)</option>
<option value="75">Film Studies (BA)</option>
<option value="76">Financial Mathematics BSc (Hons)</option>
<option value="77">Geography (BA/BSc)</option>
<option value="78">Graphic Design (BA)</option>
<option value="79">Human Biology and Infectious Diseases (BSc)</option>
<option value="80">Human Resource Management (BSc)</option>
<option value="81">Integrated Practice in Learning Disabilities Nursing and Social Work (BSc)</option>
<option value="82">Interior Design (BA)</option>
<option value="83">International Business (BSc)</option>
<option value="84">International Events Management (BSc)</option>
<option value="85">International Politics and Security (BA)</option>
<option value="86">International Relations and Politics (BA)</option>
<option value="87">Journalism (Broadcast) (BA)</option>
<option value="88">Journalism (Multimedia) (BA)</option>
<option value="89">Journalism and English (BA)</option>
<option value="90">Law (LLB)</option>
<option value="91">Law (Media and Digital Industries) (LLB)</option>
<option value="92">Law with Criminology (LLB)</option>
<option value="93">Law with Management (LLB)</option>
<option value="94">Marketing (BSc)</option>
<option value="95">Mathematics (BSc)</option>
<option value="96">Mechanical Engineering (BEng/MEng)</option>
<option value="97">Mechanical Engineering with Foundation Year (BEng)</option>
<option value="98">Media and Performance (BA)</option>
<option value="99">Media Technology (BSc)</option>
<option value="100">Medicinal Chemistry (BSc)</option>
<option value="101">Midwifery - 156 weeks (BSc)</option>
<option value="102">Midwifery - Post RN (BSc)</option>
<option value="103">Music Foundation Year (all pathways)</option>
<option value="104">Music: Creative Music Technology (BA)</option>
<option value="105">Music: Musical Arts (BA)</option>
<option value="106">Music: Popular Music and Recording (BA)</option>
<option value="107">Nursing (Adult) (BSc)</option>
<option value="108">Nursing (Children and Young People) (BSc)</option>
<option value="109">Nursing (Mental Health) (BSc)</option>
<option value="110">Nursing Theory (one year top up) (BSc)</option>
<option value="111">Occupational Therapy (BSc)</option>
<option value="112">Petroleum and Mechanical Engineering (BEng)</option>
<option value="113">Pharmaceutical Science (BSc)</option>
<option value="114">Photography (BA)</option>
<option value="115">Photography with Industry Placement (BA)</option>
<option value="116">Physics (BSc/MPhys)</option>
<option value="117">Physics with Acoustics (BSc/MPhys)</option>
<option value="118">Physics with Foundation Year (BSc)</option>
<option value="119">Physics with Studies in North America (MPhys)</option>
<option value="120">Physiotherapy (BSc)</option>
<option value="121">Podiatry (BSc)</option>
<option value="122">Politics (BA)</option>
<option value="123">Professional Broadcast Techniques (BSc)</option>
<option value="124">Professional Sound and Video Technology (BSc)</option>
<option value="125">Property and Real Estate (Accelerated Degree) (BSc)</option>
<option value="126">Property and Real Estate (BSc)</option>
<option value="127">Prosthetics and Orthotics (BSc)</option>
<option value="128">Psychology (BSc)</option>
<option value="129">Psychology and Counselling (BSc)</option>
<option value="130">Psychology and Criminology (BSc)</option>
<option value="131">Psychology of Sport (BSc)</option>
<option value="132">Public Health (BSc)</option>
<option value="133">Pure and Applied Physics (BSc)</option>
<option value="134">Quantity Surveying (Accelerated Degree) (BSc)</option>
<option value="135">Quantity Surveying (BSc)</option>
<option value="136">Social Policy (BSc)</option>
<option value="137">Social Work (BA)</option>
<option value="138">Sociology (BSc)</option>
<option value="139">Software Engineering (BSc)</option>
<option value="140">Sport Rehabilitation (BSc)</option>
<option value="141">Sports Science (Human Performance pathway) (BSc)</option>
<option value="142">Sports Science (Performance Analysis) (BSc)</option>
<option value="143">Sports Science (Strength and Conditioning pathway) (BSc)</option>
<option value="144">Television and Radio (BA)</option>
<option value="145">Theatre and Performance Practice (BA)</option>
<option value="146">Visual Arts (BA)</option>
<option value="147">Wildlife and Practical Conservation (BSc)</option>
<option value="148">Wildlife Conservation with Zoo Biology (BSc)</option>
<option value="149">Zoology (BSc)</option>
</select>
</div>
<div class="col-md-12 hide-me">
</div>
&#13;
希望有所帮助