Jquery 带有一个选项的selectbox检测选择

Jquery 带有一个选项的selectbox检测选择,jquery,html,Jquery,Html,假设我有一个选择框X,其中包含n>1如下选项: <select id="X"> <option... . . . ...</option> </select> <select id="Y"> <option value="someValue">Some Value</option> </select> 在选项更改时进行检测,以在选择框中显示与当前所选选项相

假设我有一个选择框X,其中包含n>1如下选项:

<select id="X">
    <option...
    .
    .
    .
    ...</option>
</select>
<select id="Y">
    <option value="someValue">Some Value</option>
</select>
在选项更改时进行检测,以在选择框中显示与当前所选选项相对应的视图。
但是,每当selectboxY只包含一个选项,并且用户在失去对该selectbox的关注后单击该选项时,显然,
$(“#Y”).change()将不会触发

我想要的是,当用户进行选择时,无论当前在selectbox中选择了什么,都会再次触发

以下是选择框的屏幕截图,其中包含一个选项:

因此,假设用户从框X中选择了某个选项,并且发生了一些事情,现在用户选择了框Y中唯一可用的选项。现在必须运行与
$(“#Y”)中相同的代码。这里最好的方法是什么?

我想我得换一个

$("#Y").change(/*some other code here*/);


我把这个JSFIDLE放在一起,它会在单击时触发一个更改事件,但只有在选择框打开时才会触发。只有一个问题,当用户实际更改选项时,仍然会触发单击事件

HTML


听起来您只需要一个
focus()
事件处理程序。当selectbox具有焦点时,不应该发生任何事情。仅当用户主动“选择”选项时。使用
focus()
时,我必须使用一些锅炉代码来检查所有这些条件。难道没有更优雅的处理方法吗?如果没有变化,就没有变化事件。:-)我正在搜索某种“选定”事件。单击
事件如何?
$("#Y").change(/*some other code here*/);
$(document).on("click", '#Y option', function() {
    /*some other code here*/
});
<select id="Y">
    <option value="someValue">Some Value</option>
    <option value="someOtherValue">Some Other Value</option>
</select>
//For this to work Values must be Unique.

var $isOpen = false;//flag to tell if select box is open or closed.
var $selectedItem;//The selected item before user clicks their option
$('#Y').click(function () {
    if ($isOpen) {
        $('#Y').trigger('change', true);
        $isOpen = false;
    } else {
        //this is needed as we only want to detect the users 2nd click, when selectbox is open.
        $isOpen = true;
        //set the currently selected item
        $selectedItem = $("#Y option:selected").val();
    }
});

$("#Y").blur(function () {
    //detect when user doesn't click second time and set to false
    $isOpen = false;
});

$("#Y").change(function (evt, wasTrigged) {
    //check if this change event was triggered in code
    if (wasTrigged) {
        //Check if the option value had changed from when the user clicked Select Box        
        if ($("#Y option:selected").val() === $selectedItem) {
            console.log("Select Item Never Changed");
        }
    } else {
        //When a user actually changes to a different option.
        console.log("User Changed");
    }
});