2017-07-04 83 views
0

的Javascript計算問題時,有計算的輸入值

function divideBy() { 
 
    var w = document.getElementById("first").value; 
 
    var x = document.getElementById("second").value; 
 
    var y = w/x; 
 
    y = y.toFixed(2); 
 
    var z = y; 
 
    document.getElementById("answer").innerHTML = z; 
 
    var a = document.getElementById("first1").value; 
 
    var b = document.getElementById("second1").value; 
 
    var c = a/b; 
 
    var d = c; 
 
    d = d.toFixed(2); 
 
    var e = d 
 
    document.getElementById("answer1").innerHTML = e; 
 
    document.getElementById('modal').style.visibility = 'hidden'; 
 
    document.getElementById('modal1').style.visibility = 'hidden'; 
 
}
<tr> 
 
    <td class="normal"> 
 
    <div align="right">SGST @ 9% :</div> 
 
    </td> 
 
    <td nowrap class="normal"> 
 
    <div align="right"> 
 
     <div id="divCheckbox1" style="display: none;"> 
 
     <input type="number" step=0.001 id="first" value=1 000> 
 
     <input type="number" step=0.001 id="second" value=2 .0> 
 
     </div> 
 
     <button type="button" id="modal1" onclick="divideBy()"></button> 
 
     <span id="answer"></span> 
 
     <span style="display:inline-block; width: 5;"></span> 
 
    </td> 
 
    <tr> 
 
    <td class="normal"> 
 
     <div align="right">CGST @ 9% :</div> 
 
    </td> 
 
    <td nowrap class="normal"> 
 
     <div align="right"> 
 
     <div id="divCheckbox" style="display: none;"> 
 
      <input type="number" step=0.001 id="first1" value=1 000> 
 
      <input type="number" step=0.001 id="second1" value=2 .0> 
 
     </div> 
 
     <button type="button" id="modal" onclick="divideBy()"></button> 
 
     <span id=answer1></span> 
 
     <span style="display:inline-block; width: 5;"></span> 
 
    </td>

+0

歡迎計算器!你能否澄清一下**你的問題是什麼?你期望會發生什麼,而發生什麼呢?爲了讓我們更好地爲您提供幫助,請讓我們知道更多關於**代碼的信息。如果您能讓我們知道您迄今爲止已經嘗試解決您的問題,也會有所幫助。有關詳細信息,請參閱有關[**如何提出良好問題**](http://stackoverflow.com/help/how-to-ask)的幫助文章,並參加該網站的[**遊覽**](http://stackoverflow.com/tour):) –

+0

所以問題是與 腳本心不是計算1 000作爲1000,其值爲1. –

+0

,因爲'value = 1 000'意味着屬性值= 1和一些(被忽略)屬性被稱爲'000' ... HTML非常容忍「垃圾」屬性 –

回答

0

function removeSpace(text){ 
 
     return text.replace(/\s/g,''); 
 
    } 
 
    
 
     function divideBy() { 
 
      var w = removeSpace(document.getElementById("first").value); 
 
      var x = removeSpace(document.getElementById("second").value); 
 
      var y = w/x; 
 
      y = y.toFixed(2); 
 
      var z = y; 
 
      document.getElementById("answer").innerHTML = z; 
 
      var a = removeSpace(document.getElementById("first1").value); 
 
      var b = removeSpace(document.getElementById("second1").value); 
 
      var c = a/b; 
 
      var d = c; 
 
      d = d.toFixed(2); 
 
      var e = d 
 
      document.getElementById("answer1").innerHTML = e; 
 
      document.getElementById('modal').style.visibility = 'hidden'; 
 
      document.getElementById('modal1').style.visibility = 'hidden'; 
 
     }
 
 
    <table> 
 
     <tr> 
 
      <td class="normal"> 
 
      <div align="right">SGST @ 9% :</div> 
 
      </td> 
 
      <td nowrap class="normal"> 
 
      <div align="right"> 
 
       <div id="divCheckbox1" > 
 
       <input type="text" step=0.001 id="first" value="1 000"/> 
 
       <input type="text" step=0.001 id="second" value="2 .0"/> 
 
       </div> 
 
       <button type="button" id="modal1" onclick="divideBy()">click here </button> 
 
       <span id="answer"></span> 
 
       <span style="display:inline-block; width: 5;"></span> 
 
       </div> 
 
      </td> 
 
      </tr> 
 
      <tr> 
 
      <td class="normal"> 
 
       <div align="right">CGST @ 9% :</div> 
 
      </td> 
 
      <td nowrap class="normal"> 
 
       <div align="right"> 
 
       <div id="divCheckbox"> 
 
        <input type="text" step=0.001 id="first1" value="5 000"/> 
 
        <input type="text" step=0.001 id="second1" value="3 .0"/> 
 
       </div> 
 
       <button type="button" id="modal" onclick="divideBy()">Click here1</button> 
 
       <span id=answer1></span> 
 
       <span style="display:inline-block; width: 5;"></span> 
 
       </div> 
 
      </td> 
 
      </tr> 
 
      </table> 
 
      
 

+0

我需要代碼與白色空間一起工作。它應該接受1000作爲1000 –

+0

@RalphLobo我已經更新了從數字到文本的輸入。但對於文本框控件,你需要做數字驗證 –

+0

@RalphLobo:我只是回答更新。現在您可以嘗試使用上述解決方案。 :-) –

0

就處理任何事情之前,從輸入刪除空格中的空間。

function divideBy() { 
 
       var w = document.getElementById("first").value; 
 
       var temp1=''; 
 
       for(var i=0; i < w.length; i++){ //Remove white spaces 
 
        if(w.charAt(i)!==' ') 
 
         temp1+=w.charAt(i); 
 
       } 
 
       
 
       var x = document.getElementById("second").value; 
 
       var temp2=''; 
 
       for(var i=0; i < x.length; i++){ //Remove white spaces 
 
        if(x.charAt(i)!==' ') 
 
         temp2+=x.charAt(i); 
 
       } 
 
       
 
       w=parseFloat(temp1); 
 
       x=parseFloat(temp2); 
 
       
 
       var y = w/x; 
 
       y = y.toFixed(2); 
 
       var z = y; 
 
       document.getElementById("answer").innerHTML = z; 
 
       var a = document.getElementById("first1").value; 
 
       var b = document.getElementById("second1").value; 
 
       var c = a/b; 
 
       var d = c; 
 
       d = d.toFixed(2); 
 
       var e = d 
 
       document.getElementById("answer1").innerHTML = e; 
 
       document.getElementById('modal').style.visibility = 'hidden'; 
 
       document.getElementById('modal1').style.visibility = 'hidden'; 
 
      }

+0

我需要代碼與白色空間一起工作。 –

+0

所有建議的答案都適用於空格。如果你需要你的編譯器接受空格,那麼它的錯誤要求。相反,你寫一個代碼接受空白 - 刪除它 - 然後處理輸入。 – 2017-07-04 06:55:22

+0

我試了一下代碼。它仍然將1 000的輸入值作爲1而不是1000 –

0

既然你說的數字,它只是把價值空間之前。

HTML:

<tr> 
<td class="normal"> 
<div align="right">SGST @ 9% :</div> 
</td> 
<td nowrap class="normal"> 
<div align="right"> 
    <div id="divCheckbox1" style="display: none;"> 
    <input step=0.001 id="first" value="1 000"> 
    <input step=0.001 id="second" value="2 .0"> 
    </div> 
    <button type="button" id="modal1" onclick="divideBy()"></button> 
    <span id="answer"></span> 
    <span style="display:inline-block; width: 5;"></span> 
</td> 
</td> 
</tr> 
<tr> 
<td class="normal"> 
    <div align="right">CGST @ 9% :</div> 
</td> 
<td nowrap class="normal"> 
    <div align="right"> 
    <div id="divCheckbox" style="display: none;"> 
     <input step=0.001 id="first1" value="1 000"> 
     <input step=0.001 id="second1" value="2 .0"> 
    </div> 
    <button type="button" id="modal" onclick="divideBy()"></button> 
    <span id=answer1></span> 
    <span style="display:inline-block; width: 5;"></span> 
</td> 
</td> 
</tr> 

JS:

function divideBy() { 
var w = document.getElementById("first").value ; 
var x = document.getElementById("second").value; 
var y = w.replace(" ","")/x.replace(" ",""); 
y = y.toFixed(2); 
var z = y; 
document.getElementById("answer").innerHTML = z; 
var a = document.getElementById("first1").value; 
var b = document.getElementById("second1").value; 
var c = a.replace(" ","")/b.replace(" ",""); 
var d = c; 
d = d.toFixed(2); 
var e = d 
document.getElementById("answer1").innerHTML = e; 
document.getElementById('modal').style.visibility = 'hidden'; 
document.getElementById('modal1').style.visibility = 'hidden'; 
} 
0

正如人們已經指出了意見值= 1種000不會工作,因爲我和你能將1000看作1000,但計算機不能。它假定它的值= 1(000被忽略)。所以你可以把它寫成value = 1000,並且在下一行值= 2.0。不要給空間在數字之間(因爲語言沒有允許。

而且你是不是在每個TR關閉兩個div的。

因此,爲了使代碼工作,你需要做的是改變值= 1 000根據HTML語法值= 1000和值= 2 0.0到值= 2.0,並檢查所有的元件被正確地放置。

這個片段根據JavaScript代碼的工作原理。

function divideBy() { 
 
    var w = document.getElementById("first").value; 
 
    var x = document.getElementById("second").value; 
 
    var y = w/x; 
 
    y = y.toFixed(2); 
 
    var z = y; 
 
    document.getElementById("answer").innerHTML = z; 
 
    var a = document.getElementById("first1").value; 
 
    var b = document.getElementById("second1").value; 
 
    var c = a/b; 
 
    var d = c; 
 
    d = d.toFixed(2); 
 
    var e = d 
 
    document.getElementById("answer1").innerHTML = e; 
 
    document.getElementById('modal').style.visibility = 'hidden'; 
 
    document.getElementById('modal1').style.visibility = 'hidden'; 
 
}
<table> 
 
<tr> 
 
    <td class="normal"> 
 
    <div align="right">SGST @ 9% :</div> 
 
    </td> 
 
    <td nowrap class="normal"> 
 
    <div align="right"> 
 
     <div id="divCheckbox1" style="display: none;"> 
 
     <input type="number" step=0.001 id="first" value=1 000> 
 
     <input type="number" step=0.001 id="second" value=2 .0> 
 
     </div> 
 
     <button type="button" id="modal1" onclick="divideBy()"></button> 
 
     <span id="answer"></span> 
 
     <span style="display:inline-block; width: 5;"></span> 
 
     </div> 
 
    </td> 
 
    <tr> 
 
    <td class="normal"> 
 
     <div align="right">CGST @ 9% :</div> 
 
    </td> 
 
    <td nowrap class="normal"> 
 
     <div align="right"> 
 
     <div id="divCheckbox" style="display: none;"> 
 
      <input type="number" step=0.001 id="first1" value=1000> 
 
      <input type="number" step=0.001 id="second1" value=2.0> 
 
     </div> 
 
     <button type="button" id="modal" onclick="divideBy()"></button> 
 
     <span id=answer1></span> 
 
     <span style="display:inline-block; width: 5;"></span> 
 
     </div> 
 
    </td> 
 
    </table>

+0

我同意,但有沒有辦法讓電腦接受1 000 1000 –