javascript - 如何创建一个函数以供稍后在另一个函数中重用
问题描述
我得到了这个代码:
$(document).ready(function(){
$(".nextForm").on('click',(function(){
//check criteria
if(selectedSlots.length < 1 ||$("#positionAppliedFor").get(0).value.length < 1 ||$("#maxAmountOfHours").get(0).value.length < 1){
//error messages and array
var errorForSlots= "<h5>Select at least one availability slot</h5>";
var errorForPosition = "<h5>Enter the position you wish to apply for<h5>";
var errorForHours = "<h5>Enter the amount of hours you would like to work<h5>";
var errors = [];
//add errors to array
if(selectedSlots.length < 1){errors.push(errorForSlots)};
if($("#positionAppliedFor").get(0).value.length < 1){errors.push(errorForPosition)};
if($("#maxAmountOfHours").get(0).value.length < 1){errors.push(errorForHours)};
//create message
var div = "<div id=\"sectionError\">";
if($("#sectionError").length > 0){$("#sectionError").html('')};
$(div).appendTo($(this).get(0).parentNode);
for(var i = 0; i < errors.length; i++){
$(errors[i]).appendTo($("#sectionError"));
console.log(errors[i]);}
$("</div>").appendTo($(this).get(0).parentNode);
} else {
$("#applicationDetails").slideUp();
$("#personalDetails").slideDown();
if($("#sectionError").length > 0){$("#sectionError").remove()};
}
console.log("function finished");
}));
一切都很完美,但是,我正在尝试弄清楚如何为
//create message
var div = "<div id=\"sectionError\">";
if($("#sectionError").length > 0){$("#sectionError").html('')};
$(div).appendTo($(this).get(0).parentNode);
for(var i = 0; i < errors.length; i++){
$(errors[i]).appendTo($("#sectionError"));
console.log(errors[i]);}
$("</div>").appendTo($(this).get(0).parentNode);
我计划在表单上的其他几个部分重复使用它,而不是复制/粘贴,我想获得一些帮助以使我的代码更整洁。
我确实尝试过:
function myFunction(){
//message code here
}
$(document).ready(function(){
$(".nextForm").on('click',(function(){
//check criteria
...
//add errors
...
//call func
myFunction();
(I also tried this.myFunction();)
...
}));
});
然而,这最终出现在 TypeError 中,我不知道从哪里开始......我也担心我的消息代码中的“this”,所以我也不知道如何在我的新函数中解决这个问题......
诚然,我是这方面的新手,我并不完全了解所有来龙去脉,希望您能提供帮助。
也许有更好的方法来做到这一点?不管怎样,让我知道你的想法!谢谢。
解决方案
新功能
function generateMessage(arg1) {
//create message for each section
console.log("generating message");
var div = "<div id=\"sectionError\">";
if ($("#sectionError").length > 0) {
$("#sectionError").html('')
}
;$(div).appendTo($(arg1).parent());
for (var i = 0; i < errors.length; i++) {
$(errors[i]).appendTo($("#sectionError"));
console.log(errors[i]);
}
$("</div>").appendTo($(arg1).parent());
}
更改旧功能
$(document).ready(function() {
$("#adbutnext").on('click', (function() {
//check criteria
if (selectedSlots.length < 1 || $("#positionAppliedFor").get(0).value.length < 1 || $("#maxAmountOfHours").get(0).value.length < 1) {
//error messages and array
var errorForSlots = "<h5>Select at least one availability slot</h5>";
var errorForPosition = "<h5>Enter the position you wish to apply for<h5>";
var errorForHours = "<h5>Enter the amount of hours you would like to work<h5>";
errors = [];
//add errors to array
if (selectedSlots.length < 1) {
errors.push(errorForSlots)
}
;if ($("#positionAppliedFor").get(0).value.length < 1) {
errors.push(errorForPosition)
}
;if ($("#maxAmountOfHours").get(0).value.length < 1) {
errors.push(errorForHours)
}
;
generateMessage(this);
} else {
$("#applicationDetails").slideUp();
$("#personalDetails").slideDown();
if ($("#sectionError").length > 0) {
$("#sectionError").remove()
}
;
}
console.log("function finished");
}
));
});
推荐阅读
- dns - 我需要在 cPanel 中添加验证 TXT DNS 记录,但只有 A、MX 和 CNAME 作为替代
- scala - json 到 scala 案例类
- python - 当我点击按钮时,按钮 id 应该返回给我。JSON文件
- javascript - 无法在定义的类对象中编辑数组(已解决)
- numpy - 当文档数量非常少时,在术语文档矩阵上应用 LSA
- arrays - Perl 数据清理:我怎样才能列出不重复的名称并将所有这些小列表存储在哈希中?
- python - 具有类似于 Azure Web 服务的 git 重新部署方案的 Azure 容器实例
- oracle-apex - 如何在 Oracle Apex 上为社交登录用户添加授权角色?
- javascript - Javascript 正则表达式匹配自定义标签外部/内部问题
- github-actions - 在 with 块中使用定义的环境变量进行 github 操作