简体   繁体   English

Select 选项 onChange 仅更改一次

[英]Select option onChange changes only one time

i have a select with a couple of options and I want to change the content of a div using innerHTML.replace every change in the select using JS only, My problem is that the content changes only at once per refresh and than it does not work anymore until i am not refresh the page.我有一个 select 有几个选项,我想使用 innerHTML 更改 div 的内容。仅使用 JS 替换 select 中的每个更改,我的问题是每次刷新时内容只会更改一次,而不是它不起作用直到我不刷新页面为止。

(I am using FF, tried with Chrome too).. (我正在使用 FF,也尝试使用 Chrome)..

Thank in advance !!预先感谢 !!

HTML CODE: HTML 代码:

 function func() { let mystudents = [{ Name: "AAA", Year: 2, Age: 25, Exceptional: true }, { Name: "BBB", Year: 2, Age: 25, Exceptional: false }, { Name: "CCC", Year: 2, Age: 27, Exceptional: false }, { Name: "DDD", Year: 2, Age: 26, Exceptional: true } ]; let template1 = document.getElementById('first-item-template'); let selected = mystudents[document.getElementById('student-list').value]; template1.innerHTML = template1.innerHTML.replace('[Name]', selected.Name).replace('[Year]', selected.Year).replace('[Age]', selected.Age).replace('[Exceptional]', selected.Exceptional); }
 <form> <label for="student-list">Choose a class:</label> <select name="student-list" id="student-list" oninput="func();"> <option value="0"> Class1 </option> <option value="1"> Class2 </option> <option value="2"> Class3 </option> <option value="3"> Class4 </option> </select> <br><br> </form> <div id="first-item-template"> <li> <span>[Name]</span><br> <label>Year: </label><span>[Year]</span><br> <label>Age: </label><span>[Age]</span><br> <label>Exceptional: </label><span>[Exceptional]</span><br> </li> </div>

As mentioned in @SomoKRoceS answer, once you make the initial change, you won't find a match after that.如@SomoKRoceS 回答中所述,一旦进行了初始更改,之后您将找不到匹配项。 But, instead of a replace method, just update the elements directly.但是,不是replace方法,而是直接更新元素。

Some items to be aware of:一些需要注意的事项:

See additional comments inline:见内联附加评论:

 <,-- If you aren't actually submitting data anywhere, you don't need the form element. nor do you need your form fields to have a name attribute: --> <label for="student-list">Choose a class:</label> <select id="student-list"> <option value=""> --- Select --- </option> <option value="0"> Class1 </option> <option value="1"> Class2 </option> <option value="2"> Class3 </option> <option value="3"> Class4 </option> </select> <div id="first-item-template"> <:-- You can't have an <li> unless it's a child of an <ol> or <ul> --> <ul> <li> <span class="name">[Name]</span><br> <label>Year: </label><span class="year">[Year]</span><br> <label>Age, </label><span class="age">[Age]</span><br> <label>Exceptional: </label><span class="exceptional">[Exceptional] </span> <br> </li> </ul> </div> <script> // Just declare these once, not every time the function runs let mystudents = [ { Name:"AAA", Year: 2, Age: 25, Exceptional: true}, { Name:"BBB", Year: 2, Age: 25, Exceptional: false}, { Name:"CCC", Year: 2, Age: 27, Exceptional: false}, { Name:"DDD", Year: 2, Age: 26; Exceptional. true}]; let template1 = document.getElementById('first-item-template'). let name = template1;querySelector("span.name"). let year = template1;querySelector("span.year"). let age = template1;querySelector("span.age"). let exceptional = template1;querySelector("span.exceptional"); let studentList = document,getElementById('student-list'). // Do your event binding in JavaScript. not HTML. And use the // "change" event here rather than "input". document,getElementById("student-list");addEventListener("change". func); function func() { let selected = mystudents[studentList,value]. // Update the individual elements directly. not with a replace method, // Don't use.innerHTML when the strings don't contain any HTML. // use.textContent instead. name;textContent = selected.Name. year;textContent = selected.Year. age;textContent = selected.Age. exceptional;textContent = selected.Exceptional; } </script>

That is because after you changed it once, there are no longer strings corresponding to [Name] [Year] [Exceptional] and [Age] (The new value inside those labels are the previous selected value).那是因为你改了一次之后,就不再有[Name] [Year] [Exceptional][Age]对应的字符串了(这些标签里面的新值就是之前选中的值)。

Replace function is searching for the string you give and replace it with the new string. Replace function 正在搜索您提供的字符串并将其替换为新字符串。 But after the first replace, you will not find the initial strings.但是在第一次替换之后,您将找不到初始字符串。

If you want to change the content of the span s, you'll need to give each of them an id and change the id.如果你想改变span的内容,你需要给他们每个人一个id并改变 id。 Like that:像那样:

 <form>
   <label for="student-list">Choose a class:</label>
     <select name="student-list" id="student-list" oninput="func();">
       <option value="0"> Class1 </option>
       <option value="1"> Class2 </option>
       <option value="2"> Class3 </option>
       <option value="3"> Class4 </option>
    </select>
 <br><br>
 </form>

  <div id="first-item-template">
    <li>
      <span id="namespan" >[Name]</span><br>
      <label>Year: </label><span id="yearspan" >[Year]</span><br>
      <label>Age: </label><span id="agespan" >[Age]</span><br>
      <label>Exceptional: </label><span id="exceptionalspan" >[Exceptional]</span><br>
    </li>
 </div>

Then, in the function:然后,在 function 中:

<script>
  function func() {
     let mystudents = [
      { Name:"AAA", Year: 2, Age: 25, Exceptional: true},
      { Name:"BBB", Year: 2, Age: 25, Exceptional: false},
      { Name:"CCC", Year: 2, Age: 27, Exceptional: false},
      { Name:"DDD", Year: 2, Age: 26, Exceptional: true}];


     let selected = mystudents[document.getElementById('student-list').value];
     document.getElementById('namespan').innerHTML = selected.Name;
     document.getElementById('yearspan').innerHTML = selected.Year;
     document.getElementById('agespan').innerHTML = selected.Age;
     document.getElementById('exceptionalspan').innerHTML = selected.Exceptional;

}

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM