首页 > 解决方案 > 引导模式中下一个按钮的 if 语句

问题描述

我遇到了一个快速的问题,我不知道在我的页面中为 if 给出什么条件,这样可以确保在单击下一步时模态内容从最后一个到最后一个。

$("#next-btn").on("click", function() {
  if (currentMarineKnotIndex - 1 == 0) {
    currentMarineKnotIndex = receivedArray.length - 1;
  } else {
    currentMarineKnotIndex++;
  }
  $("#marine-knot-div").html(receivedArray[currentMarineKnotIndex].nameLV + "<br>" + receivedArray[currentMarineKnotIndex].descriptionLV);
  $("#modal-header-div").css("background", "url('../Images/uploads/" + receivedArray[currentMarineKnotIndex].Image + "')");
});

$("#prev-btn").on("click", function() {
  if (currentMarineKnotIndex + 1 == 0) {
    currentMarineKnotIndex = receivedArray.length - 1;
  } else {
    currentMarineKnotIndex--;
  }
  $("#marine-knot-div").html(receivedArray[currentMarineKnotIndex].nameLV + "<br>" + receivedArray[currentMarineKnotIndex].descriptionLV);
  $("#modal-header-div").css("background", "url('../Images/uploads/" + receivedArray[currentMarineKnotIndex].Image + "')");
});

我使它适用于上一个按钮,但我不知道下一步该做什么。

标签: javascriptjquerybootstrap-modal

解决方案


你只需要改变条件..

$("#next-btn").on("click", function() {
            if(currentMarineKnotIndex-1 == receivedArray.length){
                    currentMarineKnotIndex = receivedArray.length -1;
                }
                else{
                    currentMarineKnotIndex++;
                }
            $("#marine-knot-div").html(receivedArray[currentMarineKnotIndex].nameLV+"<br>"+receivedArray[currentMarineKnotIndex].descriptionLV);
            $("#modal-header-div").css("background", "url('../Images/uploads/"+receivedArray[currentMarineKnotIndex].Image+"')");
        });
        $("#prev-btn").on("click", function() {
           if(currentMarineKnotIndex+1 == 0){
                currentMarineKnotIndex = receivedArray.length - 1;
            }else{
                currentMarineKnotIndex--;
            }
            $("#marine-knot-div").html(receivedArray[currentMarineKnotIndex].nameLV+"<br>"+receivedArray[currentMarineKnotIndex].descriptionLV);
            $("#modal-header-div").css("background", "url('../Images/uploads/"+receivedArray[currentMarineKnotIndex].Image+"')");

推荐阅读