Hallo, ich verstehe nicht warum die variablen innerhalb der zweiten Funktion nicht im alert am Ende ausgegeben werden können.
HTML
<!DOCTYPE HTML>
<html>
<body>
<form>
<input type="number" min="100.00" id="amount" name="amount" value="100.00"><br>
<form>
<input type="radio" name="g" id="g" value="1"> 1<br>
<input type="radio" name="g" id="g" value="2"> 2<br>
<input type="radio" name="g" id="g" value="3"> 3<br>
<input type="radio" name="e" id="e" value="1"> 1<br>
<input type="radio" name="e" id="e" value="2"> 2<br>
<input type="radio" name="e" id="e" value="3"> 3<br>
</form>
<input type="button" id="button" value="Show spread">
</form>
<script>
var a = document.getElementById("amount").value;
var time = document.getElementsByName("g");
var risk = document.getElementsByName("e");
var stakes = "";
var sl;
var la;
var ha;
var sh;
var sm;
var ma;
document.getElementById("button").onclick =
function validation(){
for (var i = 0, length = time.length; i < length; i++)
{
if (time[i].checked)
{
// do whatever you want with the checked radio
var b = time[i].value;
// only one radio can be logically checked, don't check the rest
break;
}
}
for (var i = 0, length = risk.length; i < length; i++)
{
if (risk[i].checked)
{
// do whatever you want with the checked radio
var c = risk[i].value;
// only one radio can be logically checked, don't check the rest
break;
}
}
if (a==null) {
alert("Please insert an amount.");
}
else if (a<100) {
alert("Amount to small.");
}
else if (b==null) {
alert("Pick a time-period.");
}
else if (c==null) {
alert("Pick a risk-level.");
}
else {
spread(a,b,c);
}
}
function spread(a,b,c) {
switch(b) {
case 1:
if(a/2<100)
{
stakes=2;
}
else{
stakes=1;
}
break;
case 2:
if(a/5<100)
{
stakes=5;
}
else{
stakes=Math.floor(a/5);
}
break;
case 3:
if(a/10<100)
{
stakes=10;
}
else{
stakes=Math.floor(a/10);
}
break;
}
switch(c) {
case 1:
sl = 0;
la = 0;
ha = 100;
sh = stakes/2;
sm = 0;
ma = 0;
break;
case 2:
sl = 1;
la = 25;
sm = 5;
ma = 50;
sh = 4;
ha = 25;
break;
case 3:
sl = stakes;
la = 100;
sm = 0;
ma = 0;
sh = 0;
ha = 0;
break;
}
alert(stakes);
}
validation();
</script>
</body>
</html>
Alles anzeigen