2013-04-02 46 views
2
<script> 
       $("select").change(function() { 
    var str = ""; 
    var price = <?php echo $row[price];?>; 
    $("select option:selected").each(function() { 
      str += $(this).text() + " "; 
    }); 
    $(".plan_price").text("price : " + parseInt(str)*price + " baht"); 
}) 
.change(); 
    </script> 

因此,我在这里得到了这个.each和.change函数,我想从这个函数中返回str的值,以便我可以使用str变量值在我的代码等地Javascript/jQuery - 如何从嵌套函数返回值

我的问题是放在哪里的回报和函数的名称,当我在其他地方把它在我的代码文件撰写一样(nameOfFunction)请帮忙

非常感谢!

托尼

+0

使用“STR”作为全局变量 – AmGates

回答

1

在这种情况下,我认为唯一的解决办法是使用一个全局变量来存储值

var myString; 
$("select").change(function() { 
    var str = ""; 
    var price = <?php echo $row[price];?>; 
    $("select option:selected").each(function() { 
       str += $(this).text() + " "; 
      }); 
    $(".plan_price").text("price : " + parseInt(str) * price + " baht"); 
    myString = str; 
}).change(); 
alert(myString)