What to consider in JS switch

Source: Internet
Author: User

Today, when doing practice code, use the switch to judge the statement, the code is as follows

<script>
function Showuser (SSS) { var shen=sss; Alert (typeof SSS); Switch (shen) {case 1: x= "Zhang San"; break; Case 2: x= "John Doe"; break; Case 3: x= "Wang er"; break; Case 4: x= "Chen"; break; } document.getElementById ("Mydiv"). Innerhtml=x;}

</script><form> Select a user:<select name= "users" onchange= "Showuser (this.value)" ><option Value= "1" >peter griffin</option><option value= "2" >lois griffin</option><option value= "3" >glenn quagmire</option><option value= "4" >joseph Swanson</option></select></form >

<div id= "Mydiv" ></div>

The code is simple, the user chooses to make a judgment, then in the Mydiv section to display the corresponding text. But how to test can not display properly, after the online review know, in the switch used in the "= = =" Judgment, that is, at the same time to judge the type of parameters, after the code can be modified to normal use.

The modified function code is as follows:

function Showuser (SSS) {    var shen=sss;    Alert (typeof SSS);    Switch (shen)      {case      "1":        x= "Today it ' s Monday";        break;      Case "2":        x= "Today it ' s Tuesday";        break;      Case "3":        x= "Today it ' s Wednesday";        break;      Case "4":        x= "Today it ' s Thursday";        break;      }    document.getElementById ("Mydiv"). Innerhtml=x;}

  

What to consider in JS switch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.