我想显示提醒讯息

时间:2017-04-10 11:26:52

标签: javascript

运行良好,但是如果所有输入值的总和不等于100并且在同一页面上停止,我想显示警告消息。



function doMath(){
  // Capture the entered values of two input boxes
  var my_input1 = document.getElementById('my_input1').value;
  var my_input2 = document.getElementById('my_input2').value;
  var my_input3 = document.getElementById('my_input3').value;
  var my_input4= document.getElementById('my_input4').value;
  var my_input5 = document.getElementById('my_input5').value;
  var my_input6 = document.getElementById('my_input6').value;
            
  // Add them together and display
  var sum = parseInt(my_input1) + parseInt(my_input2) + parseInt(my_input3) + parseInt(my_input4) + parseInt(my_input5) + parseInt(my_input6);
  document.write(sum);
}

<input type="text" id="my_input1" /></br>
<input type="text" id="my_input2" /></br>
<input type="text" id="my_input3" /></br>
<input type="text" id="my_input4" /></br>
<input type="text" id="my_input5" /></br>
<input type="text" id="my_input6" />
<input type="button" value="Add Them Together" onclick="doMath();" />
            
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:0)

&#13;
&#13;
function doMath()
{
    // Capture the entered values of two input boxes
    var my_input1 = document.getElementById('my_input1').value;
    var my_input2 = document.getElementById('my_input2').value;
    var my_input3 = document.getElementById('my_input3').value;
    var my_input4= document.getElementById('my_input4').value;
    var my_input5 = document.getElementById('my_input5').value;
    var my_input6 = document.getElementById('my_input6').value;

    // Add them together and display
    var sum = parseInt(my_input1) + parseInt(my_input2) + parseInt(my_input3) + parseInt(my_input4) + parseInt(my_input5) + parseInt(my_input6);
    if(sum >= 100){
      document.write(sum);
    }
    else{
      alert("sum is less than 100")
    }
}
&#13;
<input type="text" id="my_input1" /></br>
<input type="text" id="my_input2" /></br>
<input type="text" id="my_input3" /></br>
<input type="text" id="my_input4" /></br>
<input type="text" id="my_input5" /></br>
<input type="text" id="my_input6" />
<input type="button" value="Add Them Together" onclick="doMath();" />
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果我清楚地了解你想要的东西,你可以试试这个:

return false

我使用 if(sum!=100){ alert("Sum is not equal to 100"); }else{ document.write(sum); } 以防您想要处理结果并采取其他措施。

答案 2 :(得分:0)

您可以使用alert()函数显示警告弹出窗口

      
            function doMath()
            {
                // Capture the entered values of two input boxes
                var my_input1 = document.getElementById('my_input1').value;
                var my_input2 = document.getElementById('my_input2').value;
                 var my_input3 = document.getElementById('my_input3').value;
                var my_input4= document.getElementById('my_input4').value;
                var my_input5 = document.getElementById('my_input5').value;
                var my_input6 = document.getElementById('my_input6').value;

                // Add them together and display
                var sum = parseInt(my_input1) + parseInt(my_input2) + parseInt(my_input3) + parseInt(my_input4) + parseInt(my_input5) + parseInt(my_input6);
                
                if(sum!=100){
                  alert("Sum is not equal to 100");
                }else{
                  document.write(sum);
                }
            }

请参阅工作代码段

&#13;
&#13;
<input type="text" id="my_input1" /></br>
    <input type="text" id="my_input2" /></br>
    <input type="text" id="my_input3" /></br>
    <input type="text" id="my_input4" /></br>
        <input type="text" id="my_input5" /></br>
        <input type="text" id="my_input6" />
        <input type="button" value="Add Them Together" onclick="doMath();" />
&#13;
{{1}}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

替换

    if(sum==100) {
        document.write(sum);
    } else {
        alert("show your messaage");    
    }

import numpy as np
from skimage import io, measure, morphology

img = io.imread('img.png', as_grey=True)
img = morphology.binary_dilation(img, selem=np.ones((5,5)))

答案 4 :(得分:0)

  

这是另一种解决方案

function _get(ID){
  return document.getElementById(ID);
}

function doMath(){
  var my_input1 = _get('my_input1').value ? parseInt(_get('my_input1').value) : 0;
  var my_input2 = _get('my_input2').value ? parseInt(_get('my_input2').value) : 0;
  var my_input3 = _get('my_input3').value ? parseInt(_get('my_input3').value) : 0;
  var my_input4 = _get('my_input4').value ? parseInt(_get('my_input4').value) : 0;
  var my_input5 = _get('my_input5').value ? parseInt(_get('my_input5').value) : 0;
  var my_input6 = _get('my_input6').value ? parseInt(_get('my_input6').value) : 0;
            
  // Add them together and display
  var sum =  my_input1 + my_input2 + my_input3 + my_input4 + my_input5 + my_input6;
  if(sum==100){
    alert('Sum is = 100');
    /*YOUR CODE HERE*/
  }else if(sum<100){
    alert('Sum is less than 100');
    /*YOUR CODE HERE*/
  }else if(sum>100){
    alert('Sum is bigger than 100');
    /*YOUR CODE HERE*/
  }
}
<input type="text" id="my_input1" /></br>
<input type="text" id="my_input2" /></br>
<input type="text" id="my_input3" /></br>
<input type="text" id="my_input4" /></br>
<input type="text" id="my_input5" /></br>
<input type="text" id="my_input6" />
<input type="button" value="Add Them Together" onclick="doMath();" />
            

以下是Conditional (ternary) Operator

的详细信息