gpt4 book ai didi

javascript - 第一次通过点击事件增加值不起作用

转载 作者:行者123 更新时间:2023-12-03 08:02:43 25 4
gpt4 key购买 nike

我目前正在为每日计划创建一个选择,我有一个下一个和上一个按钮。但是,当用户第一次点击第二天时,天增量不会在第一次(点击)时增加。如果我的代码有误,请通知我。谢谢

这是 fiddle https://jsfiddle.net/5s9ca60d/1/

代码

$(function () {
var newday = 0;
$(".mobileday_selection_prev").click(function () {
var dataday = $(".mobileday_selection_day").data("day");
newday -= dataday;
if (newday <= 0) {
newday = 7;
switchselectionday(newday);
} else {
switchselectionday(newday);
}

});
$(".mobileday_selection_next").click(function () {
var dataday = $(".mobileday_selection_day").data("day");
newday += dataday;
if (newday >= 8) {
newday = 1;
switchselectionday(newday);
} else {
switchselectionday(newday);
console.log(newday);
}

});
function switchselectionday(dataday) {
switch (dataday) {
case 1:
$(".mobileday_selection_day").attr("data-day", "1");
$(".mobileday_selection_day").text("Monday");
break;
case 2:
$(".mobileday_selection_day").attr("data-day", "2");
$(".mobileday_selection_day").text("Tuesday");
break;
case 3:
$(".mobileday_selection_day").attr("data-day", "3");
$(".mobileday_selection_day").text("Wednesday");
break;
case 4:
$(".mobileday_selection_day").attr("data-day", "4");
$(".mobileday_selection_day").text("Thursday");
break;
case 5:
$(".mobileday_selection_day").attr("data-day", "5");
$(".mobileday_selection_day").text("Friday");
break;
case 6:
$(".mobileday_selection_day").attr("data-day", "6");
$(".mobileday_selection_day").text("Saturday");
break;
case 7:
$(".mobileday_selection_day").attr("data-day", "7");
$(".mobileday_selection_day").text("Sunday");
break;
}
}

});

最佳答案

你可以使用

$(function () {
var newday = 1; //<<<<<<<<<< edited
$(".mobileday_selection_prev").click(function () {
var dataday = $(".mobileday_selection_day").data("day");
--newday; //<<<<<<<<<< edited
if (newday <= 0) {
newday = 7;
switchselectionday(newday);
} else {
switchselectionday(newday);
}

});
$(".mobileday_selection_next").click(function () {
var dataday = $(".mobileday_selection_day").data("day");
++newday; //<<<<<<<<<< edited
if (newday >= 8) {
newday = 1;
switchselectionday(newday);
} else {
switchselectionday(newday);
console.log(newday);
}

});
function switchselectionday(dataday) {
switch (dataday) {
case 1:
$(".mobileday_selection_day").attr("data-day", "1");
$(".mobileday_selection_day").text("Monday");
break;
case 2:
$(".mobileday_selection_day").attr("data-day", "2");
$(".mobileday_selection_day").text("Tuesday");
break;
case 3:
$(".mobileday_selection_day").attr("data-day", "3");
$(".mobileday_selection_day").text("Wednesday");
break;
case 4:
$(".mobileday_selection_day").attr("data-day", "4");
$(".mobileday_selection_day").text("Thursday");
break;
case 5:
$(".mobileday_selection_day").attr("data-day", "5");
$(".mobileday_selection_day").text("Friday");
break;
case 6:
$(".mobileday_selection_day").attr("data-day", "6");
$(".mobileday_selection_day").text("Saturday");
break;
case 7:
$(".mobileday_selection_day").attr("data-day", "7");
$(".mobileday_selection_day").text("Sunday");
break;
}
}
});
.mobileday_selection_prev {
background-image: url('/Images/onfm/new/mobileday_prev.png');
background-position: right 19px center;
background-size: 22px;
background-repeat: no-repeat;
height: 55px;
width: 55px;
display: inline-block;
background-color: #54c6dd;
border-radius: 50%;
cursor: pointer;
vertical-align: top;
}
.mobileday_selection_next{
background-image: url('/Images/onfm/new/mobileday_next.png');
background-position: left 19px center;
background-size: 22px;
background-repeat: no-repeat;
height: 55px;
width: 55px;
display: inline-block;
background-color: #e8a327;
border-radius: 50%;
cursor: pointer;
vertical-align: top;
}
.mobileday_selection_day{
font-family: 'Montserrat';
font-size: 16px;
color: #000;
display: inline-block;
vertical-align: top;
width: 150px;
text-align: center;
line-height: 55px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mobileday_selection">
<center>
<div class="mobileday_selection_prev">prev</div>
<div class="mobileday_selection_day" data-day="1" >Monday</div>
<div class="mobileday_selection_next">next</div>
</center>
</div>

关于javascript - 第一次通过点击事件增加值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34522138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com