如何使用js将下一个先前的按钮添加到弹出窗口?

时间:2018-07-12 05:09:58

标签: javascript html css arrays

我试图在弹出框中添加下一个按钮。现在,它以特定顺序运行弹出窗口。同样,当最后一个弹出窗口打开时,该按钮将自动禁用。但是有一个问题当我删除一些模态时,代码不起作用。我想按此顺序运行弹出窗口,当我删除某些弹出窗口时,这些弹出窗口要正确运行。另外,我想创建上一个按钮。我该怎么做?请帮助我解决问题。

这是我使用的代码。

 $(document).ready(function() {
       var currentmodal = 1;
       $(".getAssignment").click(function() {
          var $divs = $(".modalDialog");
          var modal = $("*[data-modalorder="+(currentmodal++)+"]");
          if(!$("*[data-modalorder="+currentmodal+"]").length)
          {
              modal.find("input.getAssignment").prop("disabled",true);
          }
          if ($divs.length > 0 && modal) {
              window.location.href = "#" + $(modal).attr("id");
          }
       });
    });


    <input class="getAssignment" type="button" value="Open Modal">

    <div id="openModal" class="modalDialog" data-modalorder=1>
        <div>
        <input class="getAssignment" type="button" value="Previous">
        <input class="getAssignment" type="button" value="Next">
        <a href="#close" title="Close" class="close">X</a>
       <h2>Modal Box 1</h2>
            <p>This is a sample modal box that can be created using the powers of CSS3.</p>
            <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
        </div>
    </div>

    <div id="openModal2" class="modalDialog" data-modalorder=2>
        <div>   
       <input class="getAssignment" type="button" value="Previous">
       <input class="getAssignment" type="button" value="Next">
       <a href="#close" title="Close" class="close">X</a>
       <h2>Modal Box 2</h2>
            <p>This is a sample modal box that can be created using the powers of CSS3.</p>
            <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
        </div>
    </div>

https://jsfiddle.net/Sanjeewani/q1tm8ck2/10/

2 个答案:

答案 0 :(得分:0)

您在js中使用的条件始终返回true,并且数组的长度不是布尔值,因此您需要将其与整数进行比较。下面的示例:

$("*[data-modalorder="+currentmodal+"]").length==0

答案 1 :(得分:0)

$(document).ready(function() {
  $(".getAssignment2").click(function() {
   var pNode = $(this).closest(".modalDialog");
   if(pNode.prev(".modalDialog")){
     var id = pNode.prev(".modalDialog").attr("id");
     window.location.href = "#" + id;
   }
 });
 $(".getAssignment").click(function() {
  var pNode = $(this).closest(".modalDialog");
  if(pNode.next(".modalDialog")){
    var id = pNode.next(".modalDialog").attr("id");
    window.location.href = "#" + id;
  }
 });
});