如果jquery的某些条件为true,如何向变量添加特定的数字?
如果某个条件为真,我想向变量中添加一个特定的整数。 比如说如果jquery的某些条件为true,如何向变量添加特定的数字?,jquery,html,Jquery,Html,如果某个条件为真,我想向变量中添加一个特定的整数。 比如说 var x = some integer; if (some condition) add 1 to value of x 我的代码: Html <form id="f1"> <p class="Q1">CSS stand for</p><br> <input name="a1" value="A" type="checkbox" id="q1aa1" >Styling
var x = some integer;
if (some condition)
add 1 to value of x
我的代码:Html
<form id="f1">
<p class="Q1">CSS stand for</p><br>
<input name="a1" value="A" type="checkbox" id="q1aa1" >Styling Sheets<br>
<input name="a1" value="B" type="checkbox" id="q1aa2" >Style Sheets<br>
<input name="a1" value="C" type="checkbox" id="q1aa3" >Cascading style sheets<br>
<input name="a1" value="D" type="checkbox" id="q1aa4" >None of these<br>
<input type="button" id="check1" value="check1" /><br />
</form>
请帮忙
<form id="f1">
<p class="Q1">CSS stand for</p><br>
<input name="a1" value="A" type="checkbox" id="q1aa1" >Styling Sheets<br>
<input name="a1" value="B" type="checkbox" id="q1aa2" >Style Sheets<br>
<input name="a1" value="C" type="checkbox" id="q1aa3" >Cascading style sheets<br>
<input name="a1" value="D" type="checkbox" id="q1aa4" >None of these<br>
<input type="button" id="check1" value="check1" /><br />
</form>
<script>
var x = 0;
$('#check1').click(function() {
if($('#q1aa3').is(':checked')) {
x=x+1;
alert(x);
}
});
</script>
CSS代表
样式表
样式表
层叠样式表
这些都不是
var x=0;
$('#检查1')。单击(函数(){
如果($('#q1aa3')。是(':checked')){
x=x+1;
警报(x);
}
});
根据您的要求删除警报或修改变量请检查此项
$( function() {
var x = 0;
if ($('.Q1 input').is(':checked')) {
x=x+1;
}
else
{
x=x-1;
}
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
var x = 0;var cur_val;
$("#check1").click(function(){
if($('#q1aa1').is(':checked')) {
cur_val = parseInt($('#q1aa1').val());
x = x + cur_val;
$('#test').html(x);
}
if($('#q1aa2').is(':checked')) {
cur_val = parseInt($('#q1aa1').val());
x = x + cur_val;
$('#test').html(x);
}
if($('#q1aa3').is(':checked')) {
cur_val = parseInt($('#q1aa1').val());
x = x + cur_val;
$('#test').html(x);
}
if($('#q1aa4').is(':checked')) {
cur_val = parseInt($('#q1aa1').val());
x = x + cur_val;
$('#test').html(x);
}
});
});
</script>
</head>
<body>
<form id="f1">
<p class="Q1">CSS stand for</p><br>
<input name="a1" value="1" type="checkbox" id="q1aa1" >Styling Sheets<br>
<input name="a1" value="2" type="checkbox" id="q1aa2" >Style Sheets<br>
<input name="a1" value="3" type="checkbox" id="q1aa3" >Cascading style sheets<br>
<input name="a1" value="4" type="checkbox" id="q1aa4" >None of these<br>
<input type="button" id="check1" value="check1" /><br />
</form>
<div id="test"></div>
</body>
</html>
$(文档).ready(函数(){
var x=0;var cur_val;
$(“#检查1”)。单击(函数(){
如果($('#q1aa1')。是(':checked')){
cur_val=parseInt($('#q1aa1').val();
x=x+当前值;
$('#test').html(x);
}
如果($('#q1aa2')。是(':checked')){
cur_val=parseInt($('#q1aa1').val();
x=x+当前值;
$('#test').html(x);
}
如果($('#q1aa3')。是(':checked')){
cur_val=parseInt($('#q1aa1').val();
x=x+当前值;
$('#test').html(x);
}
如果($('#q1aa4')。是(':checked')){
cur_val=parseInt($('#q1aa1').val();
x=x+当前值;
$('#test').html(x);
}
});
});
CSS代表
样式表
样式表
层叠样式表
这些都不是
您只需使用此功能即可完成此操作
$(document).ready(function(){
var x = 0;var cur_val;
$(".check_class").click(function(){
cur_val = parseInt($(this).val());
x = x + cur_val;
$('#test').html(x);
});
});
<form id="f1">
<p class="Q1">CSS stand for</p><br>
<input name="a1" value="1" class="check_class" type="checkbox" id="q1aa1" >Styling Sheets<br>
<input name="a1" value="2" class="check_class" type="checkbox" id="q1aa2" >Style Sheets<br>
<input name="a1" value="3" class="check_class" type="checkbox" id="q1aa3" >Cascading style sheets<br>
<input name="a1" value="4" class="check_class" type="checkbox" id="q1aa4" >None of these<br>
<input type="button" id="check1" value="check1" /><br />
</form>
<div id="test"></div>
$(文档).ready(函数(){
var x=0;var cur_val;
$(“.check_class”)。单击(函数(){
cur_val=parseInt($(this.val());
x=x+当前值;
$('#test').html(x);
});
});
CSS代表
样式表
样式表
层叠样式表
这些都不是
我没有完全得到您想要的,这就是我给出前面代码的原因。您想将x增加1???您的问题不清楚,但它似乎真的是一个基本的编程问题。你应该努力确定…具体的整数是哪个变量?
$(document).ready(function(){
var x = 0;var cur_val;
$(".check_class").click(function(){
cur_val = parseInt($(this).val());
x = x + cur_val;
$('#test').html(x);
});
});
<form id="f1">
<p class="Q1">CSS stand for</p><br>
<input name="a1" value="1" class="check_class" type="checkbox" id="q1aa1" >Styling Sheets<br>
<input name="a1" value="2" class="check_class" type="checkbox" id="q1aa2" >Style Sheets<br>
<input name="a1" value="3" class="check_class" type="checkbox" id="q1aa3" >Cascading style sheets<br>
<input name="a1" value="4" class="check_class" type="checkbox" id="q1aa4" >None of these<br>
<input type="button" id="check1" value="check1" /><br />
</form>
<div id="test"></div>