JavaScript 根据复选框的值打开页面
JavaScript open page based on value of checkboxes
<script type="text/javascript">
$(document).ready(function() {
function updateSum() {
var total = 0;
$(".sum:checked").each(function(i, n) {total += parseInt($(n).val());})
$("#total").val(total);
}
// run the update on every checkbox change and on startup
$("input.sum").change(updateSum);
updateSum();
})
</script>
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="extra-features">
<div class="checkbox">
<label> <input name="checkbox" class="sum" id="holidays" type="checkbox" data-toggle="checkbox" value="10"/> Hide Holidays</label>
</div>
<br/>
<div class="checkbox">
<label> <input name="checkbox" class="sum" id="Governance" type="checkbox" data-toggle="checkbox" value="20"/>Hide Goverance</label>
</div>
<br/>
<div class="checkbox">
<label> <input name="checkbox" class="sum" id="bclean" type="checkbox" data-toggle="checkbox" value="50"/"/> Hide One Voice </label>
</div>
<br/>
</section>
<input id="total" type="text"/>
我正在尝试使用此代码允许用户select 从网页中选中复选框,然后根据复选框总和的值打开一个新网页。
我在想我应该使用 if 语句吗?
使用开关,这可以很容易地完成。或者创建一个查找 table,键是总和,值是 URL。但是这里有开关选项。
$(document).ready(function() {
function updateSum() {
var total = 0;
$(".sum:checked").each(function(i, n) {
total += parseInt($(n).val());
})
$("#total").val(total);
}
function triggerGo(value){
switch(value) {
case 10:
console.log("We are at ten!");
break;
case 20:
console.log("We are at twenty!");
break;
case 30:
console.log("We are at thirty!");
break;
case 50:
console.log("We are at fifty!");
break;
case 60:
console.log("We are at sixty!");
break;
case 70:
console.log("We are at seventy!");
break;
case 80:
console.log("We are at eighty!");
break;
case 0:
default:
console.log("We are at zero!");
break;
}
}
// run the update on every checkbox change and on startup
$(".go-btn").on("click", function(){
triggerGo(Number($("#total").val() ) );
});
$("input.sum").change(updateSum);
updateSum();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="extra-features">
<div class="checkbox">
<label>
<input name="checkbox" class="sum" id="holidays" type="checkbox" data- toggle="checkbox" value="10" /> Hide Holidays</label>
<button class="go-btn">
Go!
</button>
</div>
<br/>
<div class="checkbox">
<label>
<input name="checkbox" class="sum" id="Governance" type="checkbox" data-toggle="checkbox" value="20" />Hide Goverance</label>
</div>
<br/>
<div class="checkbox">
<label>
<input name="checkbox" class="sum" id="bclean" type="checkbox" data-toggle="checkbox" value="50"/" /> Hide One Voice </label>
</div>
<br/>
</section>
<input id="total" type="text" />
<script type="text/javascript">
$(document).ready(function() {
function updateSum() {
var total = 0;
$(".sum:checked").each(function(i, n) {total += parseInt($(n).val());})
$("#total").val(total);
}
// run the update on every checkbox change and on startup
$("input.sum").change(updateSum);
updateSum();
})
</script>
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="extra-features">
<div class="checkbox">
<label> <input name="checkbox" class="sum" id="holidays" type="checkbox" data-toggle="checkbox" value="10"/> Hide Holidays</label>
</div>
<br/>
<div class="checkbox">
<label> <input name="checkbox" class="sum" id="Governance" type="checkbox" data-toggle="checkbox" value="20"/>Hide Goverance</label>
</div>
<br/>
<div class="checkbox">
<label> <input name="checkbox" class="sum" id="bclean" type="checkbox" data-toggle="checkbox" value="50"/"/> Hide One Voice </label>
</div>
<br/>
</section>
<input id="total" type="text"/>
我正在尝试使用此代码允许用户select 从网页中选中复选框,然后根据复选框总和的值打开一个新网页。 我在想我应该使用 if 语句吗?
使用开关,这可以很容易地完成。或者创建一个查找 table,键是总和,值是 URL。但是这里有开关选项。
$(document).ready(function() {
function updateSum() {
var total = 0;
$(".sum:checked").each(function(i, n) {
total += parseInt($(n).val());
})
$("#total").val(total);
}
function triggerGo(value){
switch(value) {
case 10:
console.log("We are at ten!");
break;
case 20:
console.log("We are at twenty!");
break;
case 30:
console.log("We are at thirty!");
break;
case 50:
console.log("We are at fifty!");
break;
case 60:
console.log("We are at sixty!");
break;
case 70:
console.log("We are at seventy!");
break;
case 80:
console.log("We are at eighty!");
break;
case 0:
default:
console.log("We are at zero!");
break;
}
}
// run the update on every checkbox change and on startup
$(".go-btn").on("click", function(){
triggerGo(Number($("#total").val() ) );
});
$("input.sum").change(updateSum);
updateSum();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="extra-features">
<div class="checkbox">
<label>
<input name="checkbox" class="sum" id="holidays" type="checkbox" data- toggle="checkbox" value="10" /> Hide Holidays</label>
<button class="go-btn">
Go!
</button>
</div>
<br/>
<div class="checkbox">
<label>
<input name="checkbox" class="sum" id="Governance" type="checkbox" data-toggle="checkbox" value="20" />Hide Goverance</label>
</div>
<br/>
<div class="checkbox">
<label>
<input name="checkbox" class="sum" id="bclean" type="checkbox" data-toggle="checkbox" value="50"/" /> Hide One Voice </label>
</div>
<br/>
</section>
<input id="total" type="text" />