Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Web Development
You last visited: Today at 19:41

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[S] Kleine schnelle Hilfe Java Script

Discussion on [S] Kleine schnelle Hilfe Java Script within the Web Development forum part of the Coders Den category.

Reply
 
Old   #1
 
bluna771's Avatar
 
elite*gold: 4
Join Date: Sep 2009
Posts: 348
Received Thanks: 27
[S] Kleine schnelle Hilfe Java Script

Brauche für die Schule einen Taschenrechner, mein eigener will nicht so recht:


Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<script language="JavaScript">
<!--
 function ergebnis() {
      var input1 = document.getElementById("input1");
      var input2 = document.getElementById("input2");
      var input3 = document.getElementById("input3");
      var val1 = +input1.value;
      var val2 = +input2.value;
      var operate = {
          "*" : val1 * val2,
          "/" : val1 / val2,
          "+" : val1 + val2,
          "-" : val1 - val2
      };
      alert("Das ergebnis lautet: " + operate);
 }

 if (typeof operate[select.value] != "undefined") {
    input3.value = operate[select.value]
}
//-->

</script>
<noscript></noscript>

<title></title>
<meta name="author" content="user">
<meta name="editor" content="html-editor phase 5">
</head><body alink="#ff0000" bgcolor="#ffffff" text="#000000" vlink="#ff0000" link="#ff0000">

<form id="rechner">
<input size="5" id="input1" type="text">
    <select id="input3" size="1">
      <option>+</option>
      <option>-</option>
      <option>*</option>
      <option>/</option>
    </select>
<input size="5" name="input2" type="text">
 <input value="=" onclick="ergebnis()" type="button">
</form>


</body></html>
Wo ist mein Fehler?

Für eine Gute Antowort / Lösung biete ich 5 EG !
bluna771 is offline  
Old 06/11/2012, 22:49   #2
 
elite*gold: 0
Join Date: May 2010
Posts: 793
Received Thanks: 268
naja erstmal muss bei:
<input size="5" name="input2" type="text">
id statt name hin da du ja per id suchst.

und bei:
PHP Code:
      var operate = {
          
"*" val1 val2,
          
"/" val1 val2,
          
"+" val1 val2,
          
"-" val1 val2
      
}; 
weis ich nicht geanu was du da benutzt aber ich bin mir zimlich sicher das das nciht funktioniert. willst du evtl switch benutzten?
nkkk is offline  
Old 06/11/2012, 22:57   #3
 
bluna771's Avatar
 
elite*gold: 4
Join Date: Sep 2009
Posts: 348
Received Thanks: 27
Klar schlag vor was etwas bringt !! Brauche es sehr dringend
bluna771 is offline  
Old 06/12/2012, 21:12   #4


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
Arrow General Coding -> Web Development

#moved
MrSm!th is offline  
Old 06/12/2012, 21:48   #5
 
NotEnoughForYou's Avatar
 
elite*gold: 0
Join Date: Jun 2010
Posts: 3,406
Received Thanks: 2,024
ist zwar nicht optimal, hab deins nur so bearbeitet, dass es läuft:

PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html>
<
head>
<
meta http-equiv="content-type" content="text/html; charset=UTF-8">
<
script>
 function 
ergebnis() { 
 
      var 
input1 document.getElementById("input1").value;
      var 
input2 document.getElementById("input2").value;
      var 
input3 document.getElementById("input3").value;
      
      var 
val1 parseInt(input1);
      var 
val2 parseInt(input2);     
      
      var 
operate;
       
      switch(
input3) {
          case 
'*':
               
operate val1 val2;
               break;
           case 
'+':
                
operate val1 val2;
                break;
            case 
'-':
                
operate val1 val2;
                break;
           case 
'/':
                
operate val1 val2;
                break;
            default:
                
alert("Fehler");
                break;
        }    
    
      
alert("Das ergebnis lautet: " operate); 
 }

</script>
<title>Rechner</title>
<meta name="author" content="user">
<meta name="editor" content="html-editor phase 5">
</head>
<body alink="#ff0000" bgcolor="#ffffff" text="#000000" vlink="#ff0000" link="#ff0000">

<form id="rechner">
<input size="5" id="input1" type="text">
    <select id="input3" size="1">
      <option value="+">+</option>
      <option value="-">-</option>
      <option value="*">*</option>
      <option value="/">/</option>
    </select>
<input size="5" id="input2" type="text">
 <input value="=" onClick="javascript:ergebnis()" type="button">
</form>

</body>
</html> 
NotEnoughForYou is offline  
Thanks
1 User
Old 06/12/2012, 22:50   #6
 
elite*gold: 0
Join Date: Oct 2008
Posts: 319
Received Thanks: 88
Quote:
weis ich nicht geanu was du da benutzt aber ich bin mir zimlich sicher das das nciht funktioniert. willst du evtl switch benutzten?
Nennt sich JavaScript Object Notation (JSON).

PHP Code:
     var operate = {
          
"*" val1 val2,
          
"/" val1 val2,
          
"+" val1 val2,
          
"-" val1 val2
      
}; 
Du rechnest zwar speicherst dein Ergebniss aber nicht ab. Auserdem verlässt du dich auf einen global Scope deiner Variablen, keine gute Idee. Ich gaube an dem Punkt hast du JSON etwas falsch verstanden, das hinte dem Punkt ist genauso wie in PHP das "function" oder "var" statement, nur das JSON hier je nachdem was hinter dem Punkt steht separiert was was ist.

PHP Code:
var operate = {
  
"*": function (v1v2) { return v1 v2 };
}; 
In dem Fall ist "*" also eine Funktion, und wenn du mit JSON rechnen willst kommst du nicht drum herum deinen Code so zu schreiben wie in meinem Beispiel oben.
Fratyr is offline  
Thanks
1 User
Old 06/13/2012, 11:07   #7
Administrator
 
Muddy Waters's Avatar
 
elite*gold: 41364
Join Date: Jan 2010
Posts: 22,727
Received Thanks: 12,653
Bei JSON Notation dürfen allerdings keine Semikola verwendet werden, richtig wäre demnach:
Code:
var operate = {
  "*": function (v1, v2) { return v1 * v2 }
};
Ansonsten gebe ich dir aber soweit recht.
Man sollte vielleicht noch dazu sagen, dass der Zugriff auf solch eine Funktion wegen des eigentlich ungültigen Bezeichners nur auf folgende Weise erfolgen kann:
Code:
// Richtig:
operate["*"](42, 42);
// Falsch:
operate.*(42, 42);
Muddy Waters is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
kleine frage pls schnelle antwort
05/07/2012 - World of Warcraft - 6 Replies
Hay hatte ja ne lange pause und hab jetzt keine lust zu googlen gibt es noch . wenn man mit dem Freund der einen geworben hat zusammen zockt 3fache exp? edit: Gibt es tipps ausser jetzt werbe einen freund schneller zu leveln?
[Sammelthread]Kleine Java spiele
04/22/2011 - Off Topic - 2 Replies
Hey leute. Ihr kennt sowas ja sicher... Kleine selpstgescriptete JAVA spiele. Hir mal ein SAMMELTHREAD.
brauche mal ne kleine schnelle hilfe :D
08/06/2010 - 4Story - 0 Replies
Also ich fände es nett wenn mir mal jemand den normalen Tclient(kein bypass) zum downloaden postet, weil ich hab ausversen den bypass mit dem normalen download link ersetzt und nun ist der weg. da ich das spiel net neu downloaden will wärs gut wenn ihr den mal postet :D :rolleyes:
brauche schnelle hilfe bei pindel script
05/05/2010 - Diablo 2 - 2 Replies
die sorc telt etwas zuweit an pindel herran so das die gleich leavt.. kann an das so ändern das die sorc etwas weiter vorne stehen bleibt und attacke macht? ( light sorc btw)
Server time script(A very easy java script)
12/22/2008 - CO2 Private Server - 4 Replies
Put this in ur body tag <table> <td> <span id="liveclock" style="position:absolute;Change this to left or right:1;change this to bottom or top:1;"></span> <script language="JavaScript"> <!--



All times are GMT +1. The time now is 19:42.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.