模态不显示

时间:2017-05-02 10:35:37

标签: javascript modal-dialog arcgis-js-api

这是我的代码,模态不显示。我想在我的模态表单上添加所有这些属性(total_cost,J,D,E)。这就像生成报告一样 我想使用相同的按钮来调用calcost函数和显示模态。

function CalCost(a) {
  var area = a;

  var J = 0;
  var D = 0;
  var E = 0;
  var P = 0;

  var Total_Cost = 0;

  var max = 100;

  if (a == 1) {
    J = 4250;
    D = 1275;
    E = 1700;
    P = 6375;

    Total_Cost = J + D + E + P;
  } else if (a > 1) {
    var JD = 10 * a;
    J = JD * max;

    var DD = 3 * a;
    D = DD * max;

    var ED = 4 * a;
    E = ED * max;

    var PD = 15 * a;
    P = PD * max;

    Total_Cost = J + D + E + P;

    // window.prompt(" Cost : ",Total_Cost);
    document.getElementById('calValue').innerHTML = Total_Cost;

  }
}

CalCost(2)
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("Cost");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal
btn.onclick = function() {
  modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}
<div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'" style="overflow:hidden;">
  <div id="myModal" class="modal">
    <div class="modal-content">
      <span class="close">&times;</span>
      <p id='calValue'></p>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

好吧,正如我在上面的代码段中看到的那样,按钮丢失了。下面是工作代码。

据我了解,您想显示模型并在同一控件上调用 CalCost 功能。

以下是此工作代码。

Note- 我还添加了一个文本框,该文本框将动态接受 CalCost 函数中使用的值。

function CalCost(a) {
  var area = a;

  var J = 0;
  var D = 0;
  var E = 0;
  var P = 0;

  var Total_Cost = 0;

  var max = 100;

  if (a == 1) {
    J = 4250;
    D = 1275;
    E = 1700;
    P = 6375;

    Total_Cost = J + D + E + P;
  } else if (a > 1) {
    var JD = 10 * a;
    J = JD * max;

    var DD = 3 * a;
    D = DD * max;

    var ED = 4 * a;
    E = ED * max;

    var PD = 15 * a;
    P = PD * max;

    Total_Cost = J + D + E + P;

    // window.prompt(" Cost : ",Total_Cost);
    document.getElementById('calValue').innerHTML = Total_Cost;

  }
}


// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("Cost");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal
btn.onclick = function() {
var costFactor = document.getElementById("CostInput").value;
alert("Cost Factor is :- " + costFactor);
  CalCost(costFactor)
  modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
}

/* The Close Button */
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}
<div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'" style="overflow:hidden;">
  <div id="myModal" class="modal">
    <div class="modal-content">
      <span class="close">&times;</span>
      <p id='calValue'></p>
    </div>
  </div>
</div>

<button id="Cost">Show Cost</button> <input id="CostInput" type="text" name="cost" value=2>

希望这会对您有所帮助:)