单击选择选项时执行操作,但ID未知

时间:2014-02-26 19:01:20

标签: php jquery wordpress twig

我在这里遇到了jquery,wordpress和twig的大问题。

我的目标是在用户从选择中选择一个选项时显示预定义的选择。问题是我不知道检查他选择了哪个选项的方法,因为我无法通过wp_dropdown_categories获得选项值。更难的是它成为了枝条引擎,它不允许我使用每个PHP功能。

我已使用此代码解决了问题:

jQuery("#property_contract_haupt option:eq( {{ foo-1 }} )").click(function () {
                            {% for i in 0..10 %}
                            jQuery('#property_contract_no{{ i }}').hide();
                            jQuery('#input{{ i }}').hide();
                            {% endfor %}
                            jQuery('#property_contract_no{{ foo }}').show();

                            <!-- show an input if there is one for the number of foo -->
                            jQuery('#input{{ foo }}').show();
                    });

但遗憾的是,该解决方案无法在Chrome或Safari中使用,因为点击似乎在选项上不合法。我已经阅读了很多有关该问题的主题,并发现我需要使用on change事件,但我无法确定所选的选项,因为我无法获得选项的价值。

如果你能帮我找到解决这个问题的方法,我会很高兴的。

1 个答案:

答案 0 :(得分:0)

使用以下代码:

jQuery("#property_contract_haupt").on("change", function(){
    if(jQuery(this).value === "the desired option value attribute"){
        // do my desired action
    }
});