我正在从头开始学习编程和 JS。走了这么远,感觉有点尴尬,我无法弄清楚下一部分。按原样运行代码只会向您展示我的核心目的是什么。只是显示重新排列的数据。但我正在尝试添加 NEXT 和 PREVIOUS 按钮以转到选择下拉菜单中的下一个选项,以减少鼠标的里程。我还没有 PREVIOUS 按钮的任何代码/功能,因为我试图通过编写 NEXT 按钮来获得成功。您可以使用我在代码中的注释来进一步指导您。而且我没有使用 jquery。请注意,数字(商店编号)不是完全连续的。因此,“+ 1”并不是一个好主意。我将如何编码按钮以在选择标签选项中来回循环?谢谢
<body>
<p id="display1"><p/> <!--Used for debugging-->
<button type="button" onclick="previousButton()">Previous Store</button><!--Unused for now-->
<button type="button" onclick="nextButton()">Next Store</button>
<p>STORE NUMBER: <select id="mySelect" onchange="storeRequest()">
<option value="00">00
<option value="01">01
<option value="02">02
<option value="03">03
<option value="04">04
<option value="05">05
<option value="06">06
<option value="08">08
<option value="56">56
</select>
</p>
<ol id="showpages"></ol>
</body>
<script type="text/javascript">
//function below is object constructor for Page objects.
function page(name, storeNumS) {
this.name = name;
this.storeNumS = storeNumS;
this.storesArray = storeNumS.split(" ")
}
// Below are some test instances of the page objects
// JS-program's goal is to rearrange the data, then display it
var _001_01 = new page("_001_01", "01 03 05 56 06 08");
var _001_02 = new page("_001_02", "01 02 03 04 05 08");
var _002_01 = new page("_002_01", "02 03 04 56 06 08");
var _002_02 = new page("_002_02", "02 03 04 56 06 08");
// All the above pages in an array
var allPagesArray = [_001_01, _001_02, _002_01, _002_02];
// This function gets the <select> option, then runs the search function
var storeRequest = function() {
var request = document.getElementById("mySelect").value;
searchObjAry(request);
}
// Below is the function I'd like help in.
// Havent created a funciton for "Previous Button" since am
// trying to figure out "Next" Button currently.
// Hence "previousButton()" doesn't exist yet
var nextButton = function(nextRqst) {
var request = document.getElementById("mySelect").value;
var nextRqst = request + 1; // or rather goto next option on select tag list
searchObjAry(nextRqst);
// Used the line below to test the function
// document.getElementById("display1").innerHTML = nextRqst;
}
// The function below will search for requested store in every page,
// then create a list and update the DOM
var searchObjAry = function (storeNum){
var orderedPgList = "";
var pageList = [];
for (i = 0; i < allPagesArray.length; i++){
for (j = 0; j < allPagesArray[i].storesArray.length; j++){
if ( allPagesArray[i].storesArray[j] === storeNum){
pageList.push(allPagesArray[i].name);
}
}
}
for (k = 0; k < pageList.length; k++) {
orderedPgList += "<li>" + pageList[k] + "</li>";
}
document.getElementById("showpages").innerHTML = orderedPgList;
return; // <-- still have no idea what "return" really does. Not sure if it'll mess anything up later.
}
</script>
更新:2016 年 4 月 5 日。感谢 user2314727,我能够了解到“选择”可以像数组一样,它的“选项”充当索引项。昨晚在谷歌的帮助下再次摆弄我的代码,我得到了下面的解决方案。将“.value”关键字添加到@user ...的贡献中,我能够在 searchObjAry() 函数中处理选项的值。
剩下要解决的唯一问题是使 PreviousButton 循环回到列表底部并以与 NextButton 向前工作相同的方式反复向后移动。
var nextButton = function() {
var request = document.getElementById("mySelect");
var nxtIndx = request.options[request.selectedIndex += 1].value;
searchObjAry(nxtIndx);
}
var previousButton = function() {
var request = document.getElementById("mySelect");
var prevIndx = request.options[request.selectedIndex -= 1].value;
if (prevIndx !== request[0]){
searchObjAry(prevIndx);
}else if (prevIndx === request[0]){
prevIndx = request.options[request.selectedIndex = 8].value;
searchObjAry(prevIndx);
}
}