【问题标题】:JavaScript var getElementById if statement not workingJavaScript var getElementById if 语句不起作用
【发布时间】:2015-03-28 23:20:32
【问题描述】:

我对 JavaScript 比较陌生,我不知道如何在 JavaScript/html 中编写代码: 我基本上需要一个选项,以便客户可以选择他想要的语言,并根据该选择出现另一个选择列表,他可以在其中选择该语言的可用促销/项目。

<script>
function PromoLanguage(){
var language = document.getElementById('languages');
var a = language.selectedIndex;
if (a == 1){
document.getElementById('French').style.display = inline;
}
else if (a == 2){
document.getElementById('English').style.display = inline;
}
else if (a == 3){
document.getElementById('German').style.display = inline;
}
}    
</script>    
<body>
<select onchange="PromoLanguage()" id='languages'>
<option>French</option>
<option>English</option>
<option>German</option>
</select>

<select id='English' style='display: none'>
<option>ENGLISHPROMO1</option>
<option>ENGLISHPROMO2</option>
<option>...</option>

<select id='French' style='display: none'>
<option>FRENCHPROMO1</option>
<option>FRENCHPROMO2</option>
<option>...</option>

<select id='German' style='display: none'>
<option>GERMANPROMO1</option>
<option>GERMANPROMO2</option>
<option>...</option>

这是我尝试过的另一件事,如果我选择第二项(英语),它会起作用,但现在我不知道如何让它在客户选择法语或德语时做类似的事情。

document.getElementById('languages').onchange = function() {
var display = this.selectedIndex == 2 ? "Inline" : "none";
document.getElementById('English').style.display = display;
}

【问题讨论】:

  • 什么不起作用?什么问题?
  • 您应该使用小写的functionvarifelsedocument
  • 请格式化您的代码
  • 同样else () {} 不存在。
  • 你真的懂Javascript吗?

标签: javascript html if-statement var


【解决方案1】:

使用类似的东西来获取选定的选项

 $('select').on('change', function() {
    var show = $('select option:selected').val();
     if(show ==something){
    //  do something
      }
});

如果这是你的要求

对于 javascript:

         <select  id="select_id">
        <option value="0">French</option>
        <option value="1">English</option>
        </select>
       <select  id="promo" class="hidden">
        <option value="0">promo1</option>
        <option value="1">promo2</option>
        </select>
      document.getElementById('promo').style.display = 'none';

   var select_id = document.getElementById("select_id");
   var selected =select_id.options[select_id.selectedIndex].text;
     if(selected ==French){
    document.getElementById('promo').style.display = 'block';
      }

你可以做同样的事情选择促销

【讨论】:

  • 我没有看到jQuery标签,而且OP似乎是JavaScript新手,不要指望他或她知道jQuery。
  • 对@Spencer Wieczorek。我是一个新手......但不知何故我真的需要解决这个问题并且无法弄清楚。
  • @Imacange 编辑了我的 javascript 答案,希望对您有所帮助
  • '做点什么'?这就是你的解决方案?
  • @David 我不应该完成他的所有编程工作。
【解决方案2】:

请注意,我已更改您的 HTML(关闭 &lt;select&gt; 元素,并为与“语言选项”相关的元素添加类名:

<select id='languages'>
  <option>French</option>
  <option>English</option>
  <option>German</option>
</select>

<select id='English' class='languageOption'>
  <option>ENGLISHPROMO1</option>
  <option>ENGLISHPROMO2</option>
  <option>...</option>
</select>

<!-- other select elements removed for brevity -->

我还完全更改了您的功能,使其工作得更干净(尽管稍后在答案中我将解释您原始实现的问题,除了缺乏努力解决 iPad 对语法的坚持)。

这是一个看似有效的简单函数:

function PromoLanguage() {
  // I've changed the variable-names, to something meaningful,
  // which makes them easier to keep track of within the function:
  var select = document.getElementById('languages'),
    index = select.selectedIndex,
    // you don't need the <option>, you need the <option>-value,
    // if no value is set the value is the option's text:
    language = select.options[index].value;

  // using Array.prototype.forEach, and Function.prototype.call
  // to iterate over the array-like NodeList returned by 
  // document.querySelectorAll() (which is supported in IE8,
  // whereas getElementsByClassName() is not);
  Array.prototype.forEach.call(document.querySelectorAll('.languageOption'), function (opt) {
    // the first element of the anonymous function is the array-element
    // itself, in this case a DOM-node, the <select> elements found by
    // their class-name.
    // here we're setting their display property to an invalid value,
    // which removes the display property from their style attribute,
    // and allows the CSS to style them once more:
    opt.style.display = '';
  });

  // Finding the <select> element with the appropriate id,
  // and showing it:
  document.getElementById(language).style.display = 'block';
}

// in-line JavaScript is obtrusive, this binds the change
// event-handler to the element with an id='languages',
// and binds the named-function (note the lack of parentheses)
// as the event-handler:
document.getElementById('languages').addEventListener('change', PromoLanguage);

function PromoLanguage() {
  var select = document.getElementById('languages'),
    index = select.selectedIndex,
    language = select.options[index].value;
  Array.prototype.forEach.call(document.querySelectorAll('.languageOption'), function (opt) {
    opt.style.display = '';
  });
  document.getElementById(language).style.display = 'block';
}

document.getElementById('languages').addEventListener('change', PromoLanguage);
select {
  display: block;
}
select.languageOption {
  display: none;
}
<select id='languages'>
  <option>French</option>
  <option>English</option>
  <option>German</option>
</select>

<select id='English' class='languageOption'>
  <option>ENGLISHPROMO1</option>
  <option>ENGLISHPROMO2</option>
  <option>...</option>
</select>

<select id='French' class='languageOption'>
  <option>FRENCHPROMO1</option>
  <option>FRENCHPROMO2</option>
  <option>...</option>
</select>

<select id='German' class='languageOption'>
  <option>GERMANPROMO1</option>
  <option>GERMANPROMO2</option>
  <option>...</option>
</select>

您的原始代码:

Function PromoLanguage() {
  Var language = document.getElementById('languages');
  Var a = language.selectedIndex;
  If(a == 1) {
    Document.getElementById('French').style.display = display;
  }
  Else
  if (a == 2) {
    Document.getElementById('English').style.display = display;
  }
  Else
  if (a == 3) {
    Document.getElementById('German').style.display = display;
  }
}

JavaScript 区分大小写,Var 应该是varIf 应该是if;在 iPad 上打字,而不注意创建的错误是 understandable,但是发布带有额外中断的损坏代码是不好的,因为这会掩盖您自己的错误。

现在:

var a = language.selectedIndex;
if(a == 1) {

在这里,您错过了第一个选项,这将是 - 感谢 JavaScript 中的零索引 - a == 0

另外,selectedIndex 将始终是一个数字,因此您应该使用 === 来确保您正在测试完全相等(数字相同,并且两个参数都是数字)。

然后:

document.getElementById('French').style.display = display;

在这里,您将一个变量分配给元素的 display 属性;您还没有定义该属性,那么它除了引发错误之外什么都不做(希望如此)。如果您在带有错误控制台的浏览器中键入此内容(尽管据称 iPad Safari ,但我从未弄清楚如何访问它),您可以检查它报告的错误,其中会 - 希望 - 导致您遇到问题(如果您的代码事先没有被不正确的大写所阻碍)。

【讨论】:

  • 感谢@David Thomas 的关心。我真的看到你花时间关心我的问题。不幸的是,代码似乎不起作用。我真的尝试过,甚至将它复制/粘贴到 w3 编辑器 (w3schools.com/html/tryit.asp) 中,但它没有成功。一切都在显现。我确实看到我在原始帖子中弄乱了代码,而事实是我花了将近一整天的时间试图弄清楚我最后完全混淆了不同的代码并且完全混淆了。无论如何,感谢您为我的困惑带来了一些启发,我想我已经接近了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 2012-11-30
  • 1970-01-01
  • 2023-02-06
  • 1970-01-01
相关资源
最近更新 更多