douqiang6036 2014-03-14 11:19
浏览 374
已采纳

在下拉列表中触发单击事件

I have 2 dropdown lists, the first one goes as follows :

<select name="country_id" id="country_id" tabindex="1" onchange="changeLang(this);">

    {section name=jj loop=$jezyki}            

        <option value="{$jezyki[jj].code}" {if $LANG.lang == $jezyki[jj].code} selected {/if}>{$jezyki[jj].name}</option>

    {/section}  
</select>

the second one is downloaded from google server, it is a google translate widget. What i need is when the user clicks on option from the first dropdown, script should change the value of the widget dropdown list AND trigger the click function to actually translate the page.

        function changeLang(someth){
                var nLang = someth.value;
                //$(".goog-te-combo option").val(someth.value);
              //  $(".goog-te-combo").trigger('click');    
        $(".goog-te-combo option[value="+nLang+"]").prop('selected',true).click();
            }

the snippet above changes the value. I can see, for example, english selected, but still it does not translate the page

  • 写回答

3条回答 默认 最新

  • doubushi0031 2014-03-14 12:00
    关注

    You need to dispatch onchange event:

    function changeLang(someth) {
        var nLang = someth.value,
            evt = document.createEvent("HTMLEvents");
        evt.initEvent("change", false, true);
        $('.goog-te-combo').val(nLang)[0].dispatchEvent(evt);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?