使用js使html按钮更新选择标记onchange函数

use js to make html button update select tag onchange function

本文关键字:onchange 函数 选择 更新 js html 按钮 使用      更新时间:2023-10-08

按原样运行代码只会向您展示我的核心目的。只需显示重新排列的数据。但我正在尝试添加NEXT和PREVIOUS按钮,以转到选择下拉菜单中的下一个选项,从而减少鼠标上的里程。我还没有任何PREVIOUS按钮的代码/功能,因为我正试图通过对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日。感谢用户2314727,我能够了解到";选择";可以像数组一样工作;选项";充当索引项。昨晚,在谷歌的帮助下,我再次摆弄了我的代码,我得到了下面的解决方案。添加";。值";关键字到@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);
    }
    
}

也许这会有所帮助(点击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>