使用js制作html按钮更新select标签onchange函数

use js to make html button update select tag onchange function

正在从零开始学习编程和 JS。到目前为止,感觉有点尴尬,我无法弄清楚下一部分。 运行 代码将简单地向您展示我的核心目的是什么。只是显示重新排列的数据。但我正在尝试添加 NEXT 和 PREVIOUS 按钮以转到 select 下拉菜单中的下一个选项,以减少鼠标的移动距离。我还没有用于 PREVIOUS 按钮的任何 code/function,因为我试图通过编码 NEXT 按钮找到成功。您可以使用我在代码中的注释来进一步指导您。我没有使用 jquery。 请注意,数字(商店编号)并不是完全连续的。因此,“+ 1”并不是一个好主意。 我将如何编写按钮来回循环 Select 标签选项?谢谢

<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,我了解到 "select" 可以像数组一样工作,而 "options" 可以作为索引项。昨晚在 google 的帮助下再次摆弄我的代码,我得到了以下解决方案。将“.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);
    }

}

也许这会有所帮助(select 单击 nextButton 下拉列表中的下一个标签):

var request = document.getElementById("mySelect");
request.selectedIndex += 1; // goto next option on select tag list

//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");
  request.selectedIndex += 1; // 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.
}
<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>