2013-04-09 17 views
0

我用平变化如何保持函数外的变量值?

<script> 

current_fin = "none"; 
current_mat = "Pine"; 



// Pass the current selection into a variable to use. 
function getMaterial() 
{ 
    var mat = document.getElementById("dropdownone"); 
    var current_mat = mat.options[mat.selectedIndex].text; 
$("#price").html("Material is: " + current_mat + "<br/>" +"Finish is: " + current_fin); 
} 
function getFinish() 
{ 
    var fin = document.getElementById("dropdowntwo"); 
    var current_fin = fin.options[fin.selectedIndex].text; 
$("#price").html("Material is: " + current_mat + "<br/>" +"Finish is: " + current_fin); 
} 
</script> 

它所做的是写什么被选中一个div #price的价值有需要从下拉菜单中输入下面的两个功能。现在,当我改变一个,然后改变另一个时,问题就出现了。如果我在两个框之间切换,变量将恢复为默认值none和pine。我希望它们保留函数外的值,所以如果我将方框1切换到橡木,它会保持不变,直到我将其切换回来。我有一种感觉,我在这里有范围问题,但不知道如何继续。我将包含HTML以供参考。

<select name="material" id="dropdownone" onchange="getMaterial()"> 
    <option>Pine</option> 
    <option>Oak</option> 
    <option>Walnut</option> 
    <option>Plastic</option> 
</select> 

<select name="finish" id="dropdowntwo" onchange="getFinish()"> 
    <option>None</option> 
    <option>Finished</option> 
</select> 

<input type="submit" value="Submit To Cart"> 


<div id=price> 

</div> 

回答

3
<script> 

    var current_fin = "none"; 
    var current_mat = "Pine"; 


    // Pass the current selection into a variable to use. 
    function getMaterial() 
    { 
     var mat = document.getElementById("dropdownone"); 
     current_mat = mat.options[mat.selectedIndex].text; 
     $("#price").html("Material is: " + current_mat + "<br/>" +"Finish is: " + current_fin); 
    } 

    function getFinish() 
    { 
     var fin = document.getElementById("dropdowntwo"); 
     current_fin = fin.options[fin.selectedIndex].text; 
     $("#price").html("Material is: " + current_mat + "<br/>" +"Finish is: " + current_fin); 
    } 

当你写var current_mat在函数内部,它将覆盖全局current_mat

+0

哇,非常感谢!我没有意识到它是这样做的。 – 2013-04-09 05:03:11

0
<!DOCTYPE html> 
<html> 
<head> 

<script> 

current_fin = "none"; 
current_mat = "Pine"; 

function getMaterial() 
{ 
var mat = document.getElementById("dropdownone"); 
var current_mat = mat.options[mat.selectedIndex].text; 
var fin = document.getElementById("dropdowntwo"); 
var current_fin = fin.options[fin.selectedIndex].text; 
document.getElementById("price").innerHTML="Material is: " + current_mat + "<br/>" +"Finish is: " + current_fin; 
} 
function getFinish() 
{ 
var fin = document.getElementById("dropdowntwo"); 
var current_fin = fin.options[fin.selectedIndex].text; 
var mat = document.getElementById("dropdownone"); 
var current_mat = mat.options[mat.selectedIndex].text; 
document.getElementById("price").innerHTML="Material is: " + current_mat + "<br/>" +"Finish is: " + current_fin; 
} 


    </script> 
    </head> 
    <body> 

    <select name="material" id="dropdownone" onchange="getMaterial()"> 
    <option>Pine</option> 
    <option>Oak</option> 
    <option>Walnut</option> 
    <option>Plastic</option> 
    </select> 

    <select name="finish" id="dropdowntwo" onchange="getFinish()"> 
     <option>None</option> 
     <option>Finished</option> 
    </select> 

    <input type="submit" value="Submit To Cart"> 

    <div id="price"> 

    </div> 

    </body> 
    </html>