我在包含 javascript 文件时遇到了一些麻烦。我的页面上有以下代码块,我想将其移动到一个名为 cart.js 的单独文件中。问题是,每当我将所有脚本移动到该文件时,它就会停止在我的页面上工作。我尝试将整个代码块包装在准备好的文档上,但这没有用。我不知道如何包含这个。
编辑:由于查看控制台的建议,我发现了我的错误。事实证明,在 cart.js 中留下对 jquery 的调用导致了这个问题。
current_fin = "none";
current_mat = "Pine";
current_col = "Red";
current_size = "36";
jQuery(document).ready(function($) {
$("#dropdownthree").hide();
});
// Pass the current selection into a variable to use.
function getMaterial() {// function checks material and if plastic hides/shows boxes
var mat = document.getElementById("dropdownone");
current_mat = mat.options[mat.selectedIndex].text;
if (current_mat == "Plastic") {
var col = document.getElementById("dropdownthree");
current_fin = col.options[col.selectedIndex].text;
$("#dropdowntwo").hide();
$("#dropdownthree").show();
} else {
var fin = document.getElementById("dropdowntwo");
current_fin = fin.options[fin.selectedIndex].text;
$("#dropdownthree").hide();
$("#dropdowntwo").show();
}
$.post('php/productajaxtemp.php', {
ourMaterial: current_mat,
ourFinish: current_fin,
ourSize: current_size
}, function (data) {
$("#price").html(data).show();
});
}
function getFinish() {
var fin = document.getElementById("dropdowntwo");
current_fin = fin.options[fin.selectedIndex].text;
$.post('php/productajaxtemp.php', {
ourMaterial: current_mat,
ourFinish: current_fin,
ourSize: current_size
}, function (data) {
$("#price").html(data).show();
});
}
function getColor() {
var col = document.getElementById("dropdownthree");
current_col = col.options[col.selectedIndex].text;
$.post('php/productajaxtemp.php', {
ourMaterial: current_mat,
ourFinish: current_col,
ourSize: current_size
}, function (data) {
$("#price").html(data).show();
});
}
function getSize() {
var sz = document.getElementById("dropdownsize");
current_size = sz.options[sz.selectedIndex].text;
$.post('php/productajaxtemp.php', {
ourMaterial: current_mat,
ourFinish: current_col,
ourSize: current_size
}, function (data) {
$("#price").html(data).show();
});
getMaterial();
}
相关的 HTML 是
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="js/cart.js"></script>
<form action="cart.php" id="form" method="POST">
<select name="size" id="dropdownsize" onchange="getSize()">
<option>36</option>
<option>48</option>
<option>60</option>
<option>72</option>
</select>
<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>
<select name="color" id="dropdownthree" onchange="getColor()">
<option>Painted Red</option>
<option>Painted Green</option>
<option>Painted Blue</option>
<option>Painted yellow</option>
<option>Painted white</option>
<option>Primer white</option>
</select>
<input type="submit" value="Add To Cart">
</form>