Home Web Front-end JS Tutorial Example code for voting based on js_javascript skills

Example code for voting based on js_javascript skills

May 16, 2016 pm 03:47 PM
js vote

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

1. js column voting chart
Rendering:

Implementation code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>js柱状投票图</title>
<style type="text/css">
body { font-weight:bold; color:#fff; font-size:16px; font-family:Arial, Helvetica, sans-serif; background-color:#000000;}

.zhu { margin-top:100px; width:420px; position:relative;}
.good { width:200px; position:absolute; bottom:0; left:0; text-align:center; background-color:#f00; height:50px;}
.bad { width:200px;position:absolute; bottom:0; right:0; text-align:center; background-color:#009900; height:50px;}
.and {}
</style>


<script type="text/javascript">
//innerHTML 属性设置或返回表格行的开始和结束标签之间的 HTML。
//value 属性可设置或返回单选按钮的 value 属性的值。
//所有css在DOM结构里面都是字符型的 没有数字型的

function TouPiao(){
var good= parseInt(document.ballot.good_num.value);
var bad= parseInt(document.ballot.bad_num.value);
var sum= good+bad;

var sum_g=Math.round(good/sum*100);
var sum_b=Math.round(bad/sum*100);

document.getElementByIdx_x_x_x("and").innerHTML = "总数为:"+sum+"票";
document.getElementByIdx_x_x_x("g").innerHTML = "好评"+sum_g+"%";
document.getElementByIdx_x_x_x("b").innerHTML = "差评:"+sum_b+"%";

document.getElementByIdx_x_x_x("g").style.height = sum_g+"px";
document.getElementByIdx_x_x_x("g").style.marginTop = (sum_g-100)+"px";
document.getElementByIdx_x_x_x("b").style.height = sum_b+"px";
document.getElementByIdx_x_x_x("b").style.marginTop = (sum_b-100)+"px";


}
</script>
</head>

<body>
<form action="" method="get" name="ballot">
好评票数:<input name="good_num" type="text" /><br />
差评票数:<input name="bad_num" type="text" /><br />
<input name="bt" type="button" value="提交" onclick="TouPiao()" />
</form>
<div class="zhu">
<div class="good" id="g">好评50%</div>
<div class="bad" id="b">差评50%</div>
</div>
<div class="and" id="and">总数为0票</div>

</body>
</html>
Copy after login

2. JS voting
Rendering:

Implementation code:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
 <TITLE>投票 </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("请先选择您喜欢的运动");
  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("投票成功");
 setSpanWidth(lq_num, 1);
 setSpanWidth(zq_num, 2);
 setSpanWidth(ppq_num, 3);
 
 for(i = 0; i < items.length; i++){
  items[i].checked = false;
 }
 }
 
 //设置投票结果显示
 function setSpanWidth(vote_num, type){
 var _width = new Number(250*vote_num);
 var _span = document.getElementByIdx_x_x_x("span" + type);
 var _span1 = document.getElementByIdx_x_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>
 你喜欢的运动:
 <br>
 篮球 <input type="checkbox" name="item" value="1"> 足球 <input type="checkbox" name="item" value="2"> 乒乓球 <input type="checkbox" name="item" value="3">
  <br>
 <input type="button" value="提 交" onclick="vote();">
 <br>
 <br>
 <table>
 <tr>
 <td colspan=3><b>投票结果</b></td>
 </tr>
 <tr>
 <td>篮球</td>
 <td><div class="voteresult"><span id="span1" class="style3" style="width: 0px;"></span></div></td>
 <td><span id="span11">0%</span><td>
 </tr>
 <tr>
 <td>足球</td>
 <td><div class="voteresult"><span id="span2" class="style3" style="width: 0px;"></span></div></td>
 <td><span id="span12">0%</span><td>
 </tr>
 <tr>
 <td>乒乓球</td>
 <td><div class="voteresult"><span id="span3" class="style3" style="width: 0px;"></span></div></td>
 <td><span id="span13">0%</span><td>
 </tr>
</table>
 </BODY>
</HTML>

Copy after login

3. Voting
Rendering:

Implementation code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
body{ font-size:12px;}
</style>
<script language="javascript">
 function add(txt)
 {
  var abc = document.getElementByIdx_x("abc");
 abc.innerHTML = parseInt(abc.innerHTML) + 1;
 }
</script>
</head>
<body>
<table width="50" height="50" border="1" cellpadding="0" cellspacing="0">
 <tr>
  <td align="center" valign="middle"><div id="abc">0</div></td>
 </tr>
 <tr>
  <td align="center" valign="middle"><a href="#" onclick="add(1)">投一票</a></td>
 </tr>
</table>
</body>
</html>
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to initiate WeChat voting How to initiate WeChat voting Feb 23, 2024 pm 12:20 PM

How to initiate WeChat voting? You can initiate voting function in WeChat, but most users don’t know how to initiate voting function in WeChat. Next is the graphic tutorial on how to initiate WeChat voting brought by the editor. Interested users please hurry up Come and take a look! WeChat usage tutorial How to initiate WeChat voting 1. First open the WeChat APP, click on the search box at the top of the main page to enter [Voting] and click [Group Voting] as shown below; 2. Then enter the group voting applet page and click [+ Voting] service button; 3. Finally, edit the content on the create group voting page to initiate voting.

How to use JS and Baidu Maps to implement map pan function How to use JS and Baidu Maps to implement map pan function Nov 21, 2023 am 10:00 AM

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

How to use JS and Baidu Maps to implement map heat map function How to use JS and Baidu Maps to implement map heat map function Nov 21, 2023 am 09:33 AM

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

How to use JS and Baidu Map to implement map click event processing function How to use JS and Baidu Map to implement map click event processing function Nov 21, 2023 am 11:11 AM

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

See all articles