Sample Code for Voting Based on js, sample code for js Voting

Source: Internet
Author: User

Sample Code for Voting Based on js, sample code for js Voting

The example in this article describes how to implement the three voting methods in JavaScript and shares them with you for your reference. The details are as follows:

I. js columnar voting chart
:

Implementation Code:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

Ii. JS Voting
:

Implementation Code:

<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> <HTML> <HEAD> <TITLE> vote </TITLE> <meta name = "Generator" CONTENT = "EditPlus"> <meta name = "Author" CONTENT = ""> <meta name = "Keywords" CONTENT = ""> <meta name = "Description" CONTENT =" "> <mce: style> <! --. Voteresult {margin: 2px; margin-top: 5px; display: block; float: left; width: 250px; height: 10px; background-color: # EEE; overflow: hidden ;}. style3 {background-color: #666666 ;}--> </mce: style> <style mce_bogus = "1">. voteresult {margin: 2px; margin-top: 5px; display: block; float: left; width: 250px; height: 10px; background-color: # EEE; overflow: hidden ;}. style3 {background-color: #666666;} </Style> <mce: script type = "text/javascript"> <! -- Var total_vote = 0; var lq_vote = 0; var zq_vote = 0; var ppq_vote = 0; function vote () {var selectItem = 0; var items = document. getElementsByName ("item"); for (I = 0; I <items. length; I ++) {if (items [I]. checked) {selectItem ++; total_vote ++; switch (parseInt (items [I]. value) {case 1: lq_vote ++; break; case 2: zq_vote ++; break; case 3: ppq_vote ++; break ;}}} if (selectItem <= 0) {alert ("select your favorite sports first"); return;} var lq_num = new Number (lq_vote/total_vote ); var zq_num = new Number (zq_vote/total_vote); var ppq_num = new Number (ppq_vote/total_vote); alert ("voting successful"); setSpanWidth (lq_num, 1 ); setSpanWidth (zq_num, 2); setSpanWidth (ppq_num, 3); for (I = 0; I <items. length; I ++) {items [I]. checked = false ;}/// set the voting result to display function setSpanWidth (vote_num, type) {var _ width = new Number (250 * vote_num); var _ span = document. getelementbyidx_x_x ("span" + type); var _ span1 = document. getelementbyidx_x_x ("span1" + type); // alert (_ span. style. width); _ span. style. width = _ width. toFixed (0); var _ percent = new Number (100 * vote_num); _ span1.innerHTML = _ percent. toFixed (1) + "%" ;}// --> </mce: script> </HEAD> <BODY> your favorite sports: <br> basketball <input type = "checkbox" name = "item" value = "1"> football <input type = "checkbox" name = "item" value = "2"> Table Tennis <input type = "checkbox" name = "item" value = "3"> <br> <input type = "button" value = "Submit" onclick = "vote (); "> <br> <table> <tr> <td colspan = 3> <B> voting result </B> </td> </tr> <tr> <td> basketball </td> <div class = "voteresult"> <span id = "span1" class = "style3" style = "width: 0px; "> </span> </div> </td> <span id =" span11 "> 0% </span> <td> </tr> <tr> <td> football </td> <div class = "voteresult"> <span id = "span2" class = "style3" style = "width: 0px; "> </span> </div> </td> <span id =" span12 "> 0% </span> <td> </tr> <tr> <td> Table Tennis </td> <div class = "voteresult"> <span id = "span3" class = "style3" style = "width: 0px; "> </span> </div> </td> <span id =" span13 "> 0% </span> <td> </tr> </table> </BODY> </HTML>

Iii. Voting
:

Implementation Code:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

The above is all the content of this article, hoping to help you learn.

Related Article

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.