简体   繁体   English

当dropdownlist包含一个项目时,jQuery更改不会触发

[英]jQuery change does not trigger when a dropdownlist holds one item

I am trying to trigger events when options in a dropdown are clicked on. 我正在尝试在单击下拉列表中的选项时触发事件。 I couldn't find anything that explains how to do this. 我找不到任何解释如何做到这一点的东西。 What I want is to fire the event and use the value of it. 我想要的是触发事件并使用它的值。 When i use .on instead of .change I fire the event everytime the select is clicked and thats not what I want because I only want to use the value of the selected item. 当我使用.on而不是.change时,我会在每次点击选择时触发事件,这不是我想要的,因为我只想使用所选项目的值。

Assume this is dynamically generated: 假设这是动态生成的:

<select id="dropdownOne">
<option value="1">NumberOne</option>
</select>

Assume this is dynamically generated: 假设这是动态生成的:

<select id="dropdownTwo">
<option value="1">NumberOne</option>
<option value="2">NumberTwo</option>
</select>

JS: JS:

$("select").change("click", function () {
    //This event is not fired in dropdown one

    //This event is only fired in dropdown two if I change the option
    var value = $(this).val();

    if(value == 1){
        console.log("This logs number 1");
    }else{
        console.log("This logs number 2");
    }
}

How can I: 我怎样才能:

  • Trigger the event on an item in a dropdownlist that holds only one item 在仅包含一个项目的下拉列表中的项目上触发事件
  • Trigger the event on an item that is clicked multiple times 在多次单击的项目上触发事件

EDIT: So, my question was marked as a duplicate: 编辑:所以,我的问题被标记为重复:

How to call onchange event when dropdown values are same 下拉值相同时如何调用onchange事件

The "duplicate" has 6 answers: “重复”有6个答案:

Vicky Kumar: Says the code of that question is fine and working. Vicky Kumar:说这个问题的代码很好并且有效。 This answer does not contribute to my problem 这个答案对我的问题没有贡献

ajpocus: Has an answer, got edited later saying that it doesn't work. ajpocus:有一个答案,后来编辑说它不起作用。 Doesn't work 不行

Krupesh Kotecha: Uses a click event on the select, same behaviour as .on This is not what I want Krupesh Kotecha:使用选择的单击事件,与.on相同的行为这不是我想要的

Teja: Fixes the problem for that question but not mine, still doesn't trigger any event when te select only has 1 option or when you click an option more than once This is not what I want Teja:解决了这个问题的问题但不是我的问题,当te select只有1个选项或者你多次点击一个选项时仍然没有触发任何事件这不是我想要的

charu joshi: Uses .on event with :option selecter, doesn't work because the :option selecter is unknown, can also not be found in the jQuery API documentation Doesn't work charu joshi:使用.on事件:选项选择器,因为:选项选择器未知,无法在jQuery API文档中找不到,因此不起作用

derp: This answer explains how to remove duplicates and then uses the change event, this might work for the question of the "duplicate" but I never load in duplicate items in my select, and even then it still uses the change event which doesn't do what I want as explained in my question This answer does not contribute to my problem derp:这个答案解释了如何删除重复项,然后使用更改事件,这可能适用于“重复”的问题,但我从不在我的选择中加载重复的项目,即使这样,它仍然使用不会发生变化的事件如我的问题所解释的做我想做的事这个答案对我的问题没有贡献

So all of the above answers from the "duplicate" question do not apply to my problem/question. 因此,“重复”问题的所有上述答案都不适用于我的问题/问题。 Also none of those answers were marked as accepted by the original poster. 这些答案中没有一个被标记为原始海报所接受。

By definition: 根据定义:

The onchange event occurs when the value of an element has been changed. 更改元素的值时会发生onchange事件。

When there is only one option like: 当只有一个选项时:

<select id="dropdownOne">
   <option value="1">NumberOne</option>
</select>

No matter how many time you try to change/click the options, the value of select will remain same, 1 . 无论您尝试更改/单击选项多少次,select的值将保持不变, 1 And change event will never execute. 并且更改事件将永远不会执行。

And according to this answer , onSelect() and onClick() events are not supported by the <option> tag. 根据这个答案<option>标记不支持onSelect()onClick()事件。

I'd suggest you prepend an additional option with value -1 or 0 to your select. 我建议你在你的选择前面添加一个值为-1或0的附加选项。

<select id="dropdownOne">
    <option value="-1">--- Select ---</option>
    <option value="1">NumberOne</option>
</select>

Or you can create a custom select control like suggested by @Bradley Ross 或者您可以创建@Bradley Ross建议的自定义选择控件

Try this code: 试试这段代码:

var value;
$('select').focus(function () {
        value = this.value;
        if(value == 1){
            console.log("This logs number 1");
        }else{
            console.log("This logs number 2");
        }
    }).change(function() {
      value = this.value;
      if(value == 1){
        console.log("This logs number 1");
      }else{
        console.log("This logs number 2");
      }
});

Here is the jsfiddle link: https://jsfiddle.net/cdantdor/ 这是jsfiddle链接: https ://jsfiddle.net/cdantdor/

For the one item in the select drop down, I got the value focus event and after selecting item update the value by change event. 对于选择下拉列表中的一个项目,我获得了值焦点事件,并在选择项目后通过更改事件更新值。 Hopefully it will fulfill your purpose. 希望它能实现你的目的。

The following code may satisfy your need although it isn't exactly what you asked for. 以下代码可能满足您的需求,尽管它并不完全符合您的要求。 When you click on the button marked Options, it opens a little menu the vanishes when you click on Close Menu. 当您单击标记为选项的按钮时,它会打开一个小菜单,当您单击关闭菜单时,该菜单消失。 Although it isn't actually a pulldown menu, it acts as one. 虽然它实际上不是下拉菜单,但它可以作为一个菜单。

The problem is that you are trying to alter the internals of a control rather than basing actions on the control itself. 问题是你试图改变控件的内部而不是基于控件本身的操作。 Javascript has very little control over the internals of a control except as specified in the interface. 除了界面中指定的内容之外,Javascript几乎无法控制控件的内部。

Look at http://www.w3schools.com/howto/howto_js_dropdown.asp 请查看http://www.w3schools.com/howto/howto_js_dropdown.asp

 <!DOCTYPE html> <html> <head> <script> var button; var textarea; var menu; var closer; function starter() { button = document.getElementById("button"); textarea = document.getElementById("textarea"); menu = document.getElementById("menu"); closer = document.getElementById("close"); textarea.value = "Display area for selections\\r\\n"; button.onclick = open; closer.onclick = close; } function close() { menu.style.display = "none"; menu.style.visibility = "hidden"; } function open() { menu.style.display = "block"; menu.style.visibility="visible"; } function press(char) { textarea.value = textarea.value + char + "\\r\\n"; } window.onload=starter; </script> <style> #menu { display:none; border: 2px solid black; } </style> </head> <body> <p>Click on button marked <i>Options</i> to open menu for selection.</p> <form> <div id="menu"> <p>Clicking on options will cause them to appear in the text box</p> <ul> <li><input type="button" value="First" onclick="press('First');" /></li> <li><input type="button" value="Second" onclick="press('Second');" /></li> <li><input type="button" value="Third" onclick="press('Third');"</li> <li><input type="button" value="Close menu" id="close" /></li> </ul> <p>Click on <i>close menu</i> to close this section</p> </div> <p><input type="button" id="button" value="Options" /> </p> <p><textarea cols="20" rows="20" id="textarea"></textarea></p> </form> </body> </html> 

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

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