![](/img/trans.png)
[英]Passing a variable from PHP to javascript and on to an html form
[英]Passing variable values into javascript from html form
我似乎無法使它正常工作,並且已經嘗試了很多。 我有一個jscript文件,其中包含一個很長且涉及很多功能的函數,可以進行各種計算。 我正在嘗試傳遞jscript文件中存在的變量的值。 jscript文件包含3個我希望能夠從HTML表單頁面更改的變量。
我要更改的jscript文件中的變量是VehicleWeight,軸距和tireChoice。 請注意,jscript文件可以完美運行,但我希望能夠不進入而手動說var vehicleWeight = 300等的情況下更改變量。我在framework.js中運行的功能是Main。
這就是我的HTML頁面的樣子。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="framework.js"></script>
<script type="text/javascript">
function ShowCalculation() {
Main($("#wheelBaseTxt").val(), $("#tireChoiceSel").val(), $("#wheelBaseTxt").val());
}
</script>
</head>
<body>
Vehicle Weight:
<input id="vehicleWeightTxt" type="text" /><br />
Tire Choice:
<select id="tireChoiceSel">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><br />
Wheel Base: <input type="text" id="wheelBaseTxt" /><br />
<input type="submit" onclick="ShowCalculation(); return false;" />
</body>
</html>
不查看您的javascript include很難說,但是您可能需要類似於Main
函數中下面的代碼。
var vehicleWeight = 0.0;
var wheelbase = 0.0;
var tireChoice = 0.0;
function Main(inpVehicleWeight, inpWheelbase, inpTireChoice) {
vehicleWeight = inpVehicleWeight;
wheelbase = inpWheelBase;
tireChoice = inpTireChoice;
// ... other calculations, etc ...
}
除非框架js中定義的變量是全局變量,否則您無法更改它們的值。
我這樣做:
<script type="text/javascript">
function ShowCalculation() {
alert($("#wheelBaseTxt").val() + "; " + $("#tireChoiceSel").val() + "; " + $("#wheelBaseTxt").val());
}
</script>
...而且jQuery似乎沒有問題...值在那里。 使用此方法進行測試。 問題可能在Main()函數中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.