javascript

alfinete

Power Member
Código:
 <input id="Btn+" type="button" value="+" onclick="javascript:getoper(this.value);" />

 <input id="Btn-" type="button" value="-" onclick="javascript:getoper(this.value);" />


Código:
var oper;

  function getoper(valor_oper)
      {
      if (valor_oper != null)
         oper=valor_oper;
          else
             oper=oper;
        
       
      
      }

var oper vai guardar "+" ou "-" conforme o value do botão

e eu so quero que ele guarde a primeira que clique, o que ele não esta fazendo

agradecia um help
 
Se percebi bem, acho que só basta mudar a função para algo deste tipo:

PHP:
  function getoper(valor_oper){
      if (valor_oper && !oper){oper=valor_oper;}
  }
 
Queres que isso só funcione da primeira vez?...

Então é fazeres o if com ==null ou =="" e tirares o else. Como tens na função se ele carregar sempre num e noutro vai ser sempre =! de null portanto entra sempre no if e nunca no else.

Não sei se me fiz perceber.
 
thanks pelo help essa parte ja esta


code html javascript

Código:
<!DOCTYPE ht1ml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>menu</title>
    <link rel="stylesheet" type="text/css" href="menu.css" />

    <script type="text/JavaScript">
    
     var oper;
     var valor,valorf,valorf2,valorfinal1,valorfinal2,ponto;

      //danos o num escolhido     
      function getnum(valor)
      {
      //alert(valor);
     //alert(ponto);
         if(oper == null)
          {
          
       
              if (valorf==null)
              {
              valorf= valor; 
              document.getElementById('Txtoper').value=valorf; 
              }
              else
              {
               //detectaponto
               if (ponto == null)
                  {
                  valorf= valorf + "" + valor ; 
                  document.getElementById('Txtoper').value=valorf; 
                  }
                  else
                  {
                    valorf= valorf + ponto + valor ; 
                     document.getElementById('Txtoper').value=valorf;
                   
                  }
                  
              }
        
       } 
       else
       {
           if (valorf2==null)
           {
           valorf2= valor;      
           document.getElementById('Txtoper').value=valorf +"  "+oper+"  "+ valorf2;
           }
            else
            {
             //detectaponto
             if (ponto == null)
                  {
                    valorf2= valorf2 +""+ valor ;
                    document.getElementById('Txtoper').value=valorf +"  "+oper+"  "+ valorf2;
                  }
                    else
                  {
                   valorf2= valorf2 + ponto + valor ;
                    document.getElementById('Txtoper').value=valorf +"  "+oper+"  "+ valorf2;
                  }
            }   
       
       }    
      }
      
    
      //danos a operacao escolhida
      function getoper(valor_oper)
      {
      if(oper == null )
         {
          oper=valor_oper;   
          }
          else
          {
          oper=oper;
          }
      }
      
      //capta o ponto
       function getponto(ponto_c)
       {
        if(ponto == null )
         {
          ponto=ponto_c;   
          }
          else
          {
          ponto=ponto;
          }
       }
      
      
      //faz a conta comforme botão clicado chamando  get_opr(operacao)
      function calcula()
      {
      var operacao;
      valorfinal1=parseFloat(valorf);   
      valorfinal2=parseFloat(valorf2);
         
       if (oper!=null && valorf!=null && valorf2!=null)
       {
            switch (oper)
            {
            case "+":
              {
              
              operacao = valorfinal1 + valorfinal2;
              get_opr(operacao);
              break;
              }
            case "-":
              {
              operacao = valorfinal1 - valorfinal2;
              get_opr(operacao);
              break;
              }
            
             case "x":
              {
              operacao = valorfinal1 * valorfinal2;
              get_opr(operacao);
              break;
              }
              case "/":
              {
              operacao = valorfinal1 / valorfinal2;
              get_opr(operacao);
              break;
              }
            }
        }
        else
         {
         document.getElementById('lblerro').value=  "obrigatório escolher operação";
         }
       
      }
      
      // da resultado da operacao
      function get_opr(operacao)
      {
      
       //document.getElementById('Txtoper').value= " "+ valorfinal1 + " " + oper + " " + valorfinal2 ;
       document.getElementById('Txtres').value= operacao;
       document.getElementById('lblerro').value= "";
       document.getElementById('conta').style.display = "none";
      }
      
      //limpa dados
      function limpa()
      {
       oper = null;
       valorf=null;
       valorf2=null;
       valorfinal1=null;
       valorfinal2=null;
       ponto=null;
       document.getElementById('Txtoper').value= "";
       document.getElementById('Txtres').value= "";
       document.getElementById('conta').style.display = "inline";
      }
      
      
      
    </script>

</head>
<body>
    <div class="wraper">
        <div class="menuprinc">
            <table id="conta">
                <tr>
                    <td>
                        <input style="width:25px ;height:25px" id="Btnc1" type="button" value="1" onclick="javascript:getnum(this.value);" />
                    </td>
                    <td>
                        <input style="width:25px ;height:25px" id="Btnc2" type="button" value="2" onclick="javascript:getnum(this.value);" />
                    </td>
                    <td>
                        <input style="width:25px ;height:25px" id="Btnc3" type="button" value="3" onclick="javascript:getnum(this.value);" />
                    </td>
                    <td>
                        <input style="width:25px ;height:25px" id="Btn-" type="button" value="-" onclick="javascript:getoper(this.value);" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input style="width:25px ;height:25px" id="Btnc4" type="button" value="4" onclick="javascript:getnum(this.value);" />
                    </td>
                    <td>
                        <input style="width:25px ;height:25px" id="Btnc5" type="button" value="5" onclick="javascript:getnum(this.value);" />
                    </td>
                    <td>
                        <input style="width:25px ;height:25px" id="Btnc6" type="button" value="6" onclick="javascript:getnum(this.value);" />
                    </td>
                    <td>
                        <input style="width:25px ;height:25px" id="Btn+" type="button" value="+" onclick="javascript:getoper(this.value);" />
                    </td>
                </tr>
                
                <tr>
                    <td>
                        <input style="width:25px ;height:25px" id="Btnc7" type="button" value="7" onclick="javascript:getnum(this.value);" />
                    </td>
                    <td>
                        <input style="width:25px ;height:25px" id="Btnc8" type="button" value="8" onclick="javascript:getnum(this.value);" />
                    </td>
                    <td>
                        <input style="width:25px ;height:25px" id="Btnc9" type="button" value="9" onclick="javascript:getnum(this.value);" />
                    </td>
                    <td>
                        <input style="width:25px ;height:25px" id="btn_prod" type="button" value="x" onclick="javascript:getoper(this.value);" />
                    </td>
                </tr>
                <tr>
                    <td colspan ="2" >
                        <input style="width:65px ;height:25px" id="Btnc0" type="button" value="0" onclick="javascript:getnum(this.value);" />
                    </td>
                      <td >
                        <input style="width:25px ;height:25px" id="btnponto" type="button" value="." onclick="javascript:getponto(this.value);" />
                    </td>
                    <td >
                        <input style="width:25px ;height:25px" id="btn_div" type="button" value="/" onclick="javascript:getoper(this.value);" />
                    </td>
                </tr>
             
            </table>
            <table>
           
               <tr>
             
                    <td colspan ="2" >
                     <input style="width:65px ;height:25px" id="btn_igual" type="button" value="=" onclick="javascript:calcula();" />
                    </td>
                    <td colspan ="2" >
                       <input style="width:55px ;height:25px" id="btnlimpa" type="button" value="limpar" onclick="javascript:limpa();" />
                    </td>
                </tr>
          
            
            </table>
            <table>
                <tr>
                    
                       <td>
                        <input readonly="readonly"  id="Txtoper" type="text" style="width: 100px;" />
                    </td>
                    <td>
                        <input readonly="readonly" id="Txtres" type="text" style="width: 50px;" />
                    </td>
                   
                </tr>
                <tr>
                    <td rowspan="4" >
                        <label id="lblerro"  style="width: 100px;color:White; " ></label>
                    </td>
                    
                </tr>
            </table>
        </div>
    </div>
</body>
</html>


code css

Código:
* { padding: 0; margin: 0; }

body
{
}
.wraper
{
position:relative;
margin: 0 auto;
top:10px; 
width:800px;
height:650px;
background-color:Gray;  
}
 
Back
Topo