2017-07-04 1 views
0

Javascript problème de calcul quand il y a un espace au cours de calcul de la valeur d'entrée

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

Bienvenue dans StackOverflow! Pouvez-vous s'il vous plaît clarifier exactement ** quel ** est votre problème? Qu'attendez-vous qu'il se passe, et que se passe-t-il à la place? Afin que nous puissions mieux vous aider, s'il vous plaît laissez-nous savoir plus ** sur ** votre code. Il serait également utile que vous nous fassiez savoir ce que vous avez essayé jusqu'ici pour résoudre votre problème. Pour plus d'informations, référez-vous à l'article d'aide concernant [** comment poser de bonnes questions **] (http://stackoverflow.com/help/how-to-ask), et prenez la [** visite du site **] (http://stackoverflow.com/tour) :) –

+0

Donc le problème est avec le script ne calcule pas 1 000 comme 1000, son considérant la valeur comme 1. –

+0

parce que 'value = 1 000' signifie la valeur d'attribut = 1 et un certain attribut (ignoré) appelé' 000' ... HTML est très indulgent avec les attributs "garbage" –

Répondre

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

J'aurais besoin du code pour travailler avec les espaces blancs. il devrait accepter 1 000 comme 1000 –

+0

@RalphLobo J'ai mis à jour l'entrée du nombre au texte. mais pour le contrôle de la zone de texte, vous devez effectuer une validation numérique. –

+0

@RalphLobo: Je viens de répondre à la mise à jour. Maintenant, vous pouvez essayer avec la solution ci-dessus. :-) –

0

Il suffit de supprimer les espaces blancs de l'entrée avant de traiter quoi que ce soit.

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

Je voudrais le code pour travailler avec les espaces blancs. –

+0

Toutes les réponses suggérées fonctionnent avec des espaces blancs. Si vous avez besoin de votre compilateur pour accepter les espaces vides, alors sa mauvaise exigence. Au lieu de cela, vous écrivez un code qui accepte les espaces vierges - le supprime - puis traite l'entrée. –

+0

J'ai essayé le code.il condering toujours la valeur d'entrée de 1 000 comme 1 mais pas 1000 –

0

Puisque vous avez dit son numéro, sa juste valeur avant de prendre la place.

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

Comme l'ont signalé dans les commentaires valeur = 1 000 ne fonctionnera pas, parce que moi et vous pouvez voir 1 000 comme 1000 mais l'ordinateur ne peut pas. Il l'assume comme valeur = 1 (000 est ignoré). Vous pouvez donc l'écrire comme valeur = 1000 ans et aussi dans la ligne suivante valeur = 2.0. Ne donnez pas des espaces entre les chiffres (car la langue ne le permet.

Aussi, vous ne fermons pas les deux divs dans chaque tr.

Donc, pour faire le travail de code, tout ce que vous devez faire est changer la valeur = 1 000 à la valeur = 1000 et la valeur = 2 .0 à la valeur = 2.0, et vérifier tous les éléments sont correctement placés selon la syntaxe HTML.

Cet extrait fonctionne selon votre code 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

je suis d'accord, mais est-il un moyen que je peux faire l'ordinateur accepter 1 000 comme 1000 –