If..else.. 带有选择框值 javascript 和 html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14094590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:42:33  来源:igfitidea点击:

If..else.. with selection box value javascript and html

javascripthtml

提问by Ryan Fitzgerald

I have a selection box with three different options (4 really with the blank one) and after I click a button I need an alert box to display a message. Here's some code.

我有一个包含三个不同选项的选择框(4 个实际上是空白的),在单击按钮后,我需要一个警告框来显示消息。这是一些代码。

HTML:

HTML:

<select id="item1" name="Item 1">
      <option></option>
      <option value="1">Camera</option>
      <option value="2">Microphone</option>
      <option value="3">Tripod</option>
    </select>

    <button onclick="message()">Go!</button>

Javascript:

Javascript:

    <SCRIPT language = javascript>

function message() {

var s = document.getElementById('item1');
var item1 = s.options[s.selectedIndex].value;

if (item1 = 1) {
    alert("it equals camera")
}
else if (item1 = 2) {
    alert("it equals microphone")
}
else if (item1 = 3) {
    alert("it equals tripod")
}
}

</SCRIPT>

Every time I click the button the alert box says "it equals camera". I could select Microphone and click the button and it would still say that.

每次我单击按钮时,警报框都会显示“它等于相机”。我可以选择麦克风并单击按钮,它仍然会这样说。

If I put

如果我把

alert(item1)

in the function it displays 1, 2, or 3. So I'm assuming it's something with the if..else.. statements.

在函数中它显示 1、2 或 3。所以我假设它与 if..else.. 语句有关。

回答by Raghvendra Parashar

In JavaScript we should use ===, this checks data typealso.

在 JavaScript 中,我们应该使用===,这也会检查数据类型

回答by No_Name

Remember using ==instead of =

记住使用==代替=

if(item == 1)

instead of

代替

if(item = 1)

回答by ThiefMaster

In JavaScript (any pretty much every other curly-braces-language) the single =always means assignments. So you assign the value 1to item1.

在 JavaScript(几乎所有其他花括号语言)中, single=总是意味着assignments。因此,您将值分配1item1.

You want the comparisonoperator which is ==.

您需要比较运算符,即==.

function message() {
    var s = document.getElementById('item1');
    var item1 = s.options[s.selectedIndex].value;

    if(item1 == '1') {
        alert("it equals camera")
    } else if(item1 == '2') {
        alert("it equals microphone")
    } else if(item1 == '3') {
        alert("it equals tripod")
    }
}

Here are some other suggestions to improve your code:

以下是一些改进代码的其他建议:

  • Do not use the deprecated languageattribute of <script>. Just <script>is fine for JavaScript or <script type="text/javascript>if you want to be explicit.
  • Do not use inline events. Instead of the onclick="..."register an event handler using the addEventListener()method.
  • 不要使用 的弃用language属性<script>。仅<script>适用于 JavaScript 或<script type="text/javascript>如果您想明确表示。
  • 不要使用内联事件。而不是onclick="..."使用该addEventListener()方法注册事件处理程序。

回答by Denys Séguret

Replace

代替

if (item1 = 1) {

with

if (item1 == 1) {

(and the same for the other ones)

(其他的也一样)

item = 1changes the value of item1and returns 1, which evaluates as truein a test.

item = 1更改item1和 返回的值1,其计算结果与true测试一样。

But note that you could more efficiently

但请注意,您可以更有效地

  • use a switch
  • or read the value directly
  • 使用开关
  • 或直接读取值

For example :

例如 :

document.getElementById('item1').onchange = function(){
    alert("it equals " + this.options[this.selectedIndex].innerHTML);        
}????????

Demonstration

示范

回答by P1nGu1n

Why not using this method, it's easier when your selector gets more items?

为什么不使用这种方法,当您的选择器获得更多项目时更容易?

var s = document.getElementById("item1");
var item1 = s.options[s.selectedIndex].text;
window.alert('it equals '+item1);

Edit:JSFiddle

编辑:JSFiddle

Edit 2:Changing =to ==solves your problem. And instead of using s.options[s.selectedIndex].valueyou can simply use s.selectedIndex. This will also return 1, 2 or 3 and this is easier to understand.

编辑 2:更改===解决您的问题。而不是使用,s.options[s.selectedIndex].value您可以简单地使用s.selectedIndex. 这也将返回 1、2 或 3,这更容易理解。