合并两个JavaScript脚本 [英] Merge two JavaScript scripts
问题描述
我有两个JavaScript代码段。当点击更新按钮时,这些会执行特定的任务。
I have two JavaScript code snippets. These perform specific task when an 'Update' button is clicked.
我想合并它们。
JavaScript 1:点击按钮时,它会检查是否至少选中了一个复选框:
JavaScript 1: When the button is clicked, it checks if at least one checkbox is selected:
function doUpdate(){
var c = document.getElementsByTagName('input');
for (var i = 0; i < c.length; i++) {
if (c[i].type == 'checkbox' && c[i].checked == true) {
// At least one checkbox is checked
document.holiDay.command.value= 'update';
document.holiDay.submit();
return true;
}
}
// Nothing has been checked
alert("Please identify what warehouses comply:");
return false;
}
JavaScript 2:当选中任何复选框并点击更新按钮时,或取消选中所有复选框(如果未选中任何复选框);然后执行更新功能:
JavaScript 2: When any checkbox is checked and update button is clicked, check all of them or uncheck all of them if any checkbox is unchecked; then perform the update feature:
function doUpdate(){
checked=false;
function All (holiDay) {
var all= document.getElementById('holiDay');
if (checked == false){
checked = true
}
else{
checked = false
}
for (var i =0; i < all.elements.length; i++){
all.elements[i].checked = checked;
}
}
//after checked or unchecked all checkboxes then submit the form and other functionality
document.holiDay.command.value= 'update';
document.holiDay.submit();
return true;
}
推荐答案
你想做什么,但这里有一个刺:
I'm really not sure what you want to do, but here's a stab at it:
function doUpdate(){
var c = document.getElementsByTagName('input');
for (var i = 0; i < c.length; i++) {
if (c[i].type == 'checkbox' && c[i].checked == true) {
// At least one checkbox is checked
UpdateHoliday();
return true;
}
}
// Nothing has been checked
alert("Please identify what warehouses comply:");
return false;
}
function UpdateHoliday(){
checked = false;
function All (holiDay) {
var all = document.getElementById('holiDay');
checked = !checked;
for (var i =0; i < all.elements.length; i++){
all.elements[i].checked = checked;
}
}
//after checked or unchecked all checkboxes then submit the form and other functionality
document.holiDay.command.value = 'update';
document.holiDay.submit();
}
这将真正有助于简化和缩进你的代码,更清楚。
It would really help to simplify and indent your code so that we could understand it more clearly.
这篇关于合并两个JavaScript脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!