简体   繁体   English

这个js代码不起作用,我需要另一双眼睛

[英]this js code doesnt work and i need another pair of eyes here

I am trying to add selected options to tag using js. 我正在尝试使用js添加选定的选项以进行标记。 I try adding embedded code on "Shopify" and it doesn't change the options in the other . 我尝试在“Shopify”上添加嵌入代码,但它不会更改另一个中的选项。 When using w3school editor it works. 当使用w3school编辑器时它可以工作。 Any idea why it doesn't work here? 知道为什么它在这里不起作用吗?

<div style="text-align:center;">
  <table border="2">
    <tbody>
      <tr>
        <td>
          <select onchange="genderChoose()" id="userGender"> 
            <option value="null">Choose gender</option><option value="1">Women</option>
            <option value="Men">Men</option> 
          </select>
        </td>
        <td>
          <select id="category">
            <option value="Choose category">Choose category
            </option>
          </select>
        </td>
        <td>
          <input type="text"><input id="searchygolide" type="submit" 
          value="Ygolide it !">
        </td>

      </tr>
    </tbody>
  </table>
  <script>
    function genderChoose(){
      if (document.getElementById("userGender").value == 1) 
      {
        document.getElementById("category").innerHTML = '<option 
        value="shirts">shirts</option><option value ="tops">tops</option>'
      }
    }
  </script>
</div>

There appears to be a line break in the middle of the string you're trying to set to your #category element's innerHTML , right before value="shirts" . 在您尝试设置为#category元素的innerHTML的字符串中间似乎存在换行符,就在value="shirts" You should see an error in your console because of it. 您应该会在控制台中看到错误。 Remove the line break to fix the string. 删除换行符以修复字符串。

 <script> function genderChoose(){ if (document.getElementById("userGender").value == 1) { document.getElementById("category").innerHTML = '<option value="shirts">shirts</option><option value ="tops">tops</option>' } } </script> 

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

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