JQuery 测验应用程序 - 使用;用于打开/关闭变量的标签
我正在编写一个 jqueryphonegap 测验应用程序,并且有许多类别,用户可以通过复选框进行选择。然后返回属于这些类别的相关问题。但是,如果选择了该类别的复选框,我有两个巨大的 switch 语句将相关变量从 0 更改为 1,反之亦然(此信息用于构建复合数据库查询)。
复选框后面的变量值只能是 0 或 1,那么有没有更好的方法来做到这一点?
我的 HTML 是:
<h2>Categories</h2>
<ul class="rounded">
<li>Cardiology<span class="toggle"><input type="checkbox" id="cardiology" /></span></li>
<li>Respiratory<span class="toggle"><input type="checkbox" id="respiratory" /></span></li>
<li>Gastrointestinal<span class="toggle"><input type="checkbox" id="gastrointestinal" /></span></li>
<li>Neurology<span class="toggle"><input type="checkbox" id="neurology" /></span></li>
</ul>
我的 Javascript 是这样的:
var toggle_cardiology = 0;
var toggle_respiratory = 0;
var toggle_gastrointestinal = 0;
var toggle_neurology = 0;
$(function() {
$('input[type="checkbox"]').bind('click',function() {
if($(this).is(':checked'))
{
switch (this.id)
{
case "cardiology":
toggle_cardiology = 1;
break;
case "respiratory":
toggle_respiratory = 1;
break;
case "gastrointestinal":
toggle_gastrointestinal = 1;
break;
case "neurology":
toggle_neurology = 1;
break;
等等(这对于 10 多个类别加上一个带有开关的 else 语句来将它们改回来很麻烦)
我正在考虑将 HTML id 标签连接到“toggle_”前缀 - 用伪代码表示:
if (toggle_ + this.id == 1){
toggle_ + this.id == 0}
if (toggle_ + this.id == 0){
toggle_ + this.id == 1}
谢谢,尼克。
I am writing a jquery phonegap quiz app and have a number of categories from which a user can select via checkbox. Relevant questions belonging to those categories are then returned. However, I have two huge switch statements to change the relevant variables from 0 to 1 if the checkbox for that category is selected and vice versa (this info is used to build a compound db query).
The value of the variable behind the checkbox is only ever 0 or 1, so is there a better way to do this?
My HTML is:
<h2>Categories</h2>
<ul class="rounded">
<li>Cardiology<span class="toggle"><input type="checkbox" id="cardiology" /></span></li>
<li>Respiratory<span class="toggle"><input type="checkbox" id="respiratory" /></span></li>
<li>Gastrointestinal<span class="toggle"><input type="checkbox" id="gastrointestinal" /></span></li>
<li>Neurology<span class="toggle"><input type="checkbox" id="neurology" /></span></li>
</ul>
My Javascript is along the lines of:
var toggle_cardiology = 0;
var toggle_respiratory = 0;
var toggle_gastrointestinal = 0;
var toggle_neurology = 0;
$(function() {
$('input[type="checkbox"]').bind('click',function() {
if($(this).is(':checked'))
{
switch (this.id)
{
case "cardiology":
toggle_cardiology = 1;
break;
case "respiratory":
toggle_respiratory = 1;
break;
case "gastrointestinal":
toggle_gastrointestinal = 1;
break;
case "neurology":
toggle_neurology = 1;
break;
etc. (which is cumbersome with 10+ categories plus an else statement with a switch to change them back)
I'm thinking of something along the lines of concatenating the HTML id tag onto the "toggle_" prefix - in pseudocode:
if (toggle_ + this.id == 1){
toggle_ + this.id == 0}
if (toggle_ + this.id == 0){
toggle_ + this.id == 1}
Thanks, Nick.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$(this).is(':checked') 返回 true/false。使用双位非运算符,您将得到 1 或 0。
编辑:您应该使用一个对象来存储切换状态,以便您可以动态访问它们。
$(this).is(':checked') returns true/false. Using the double bitwise NOT operator, you get 1 or 0.
edit: You should use an object to store the toggle states so you can access them dynamically.
动态变量?
编辑:
缩短:
Dynamic variables?
EDIT:
Shortened: