douqiang6036
2014-03-14 11:19
浏览 305

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

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

图片转代码服务由CSDN问答提供 功能建议

我有2个下拉列表,第一个如下:

  &lt; select name =“country_id”id =“country_id”tabindex =“1”onchange =“changeLang(this);”&gt; 
 
 {section name = jj loop = $ jezyki} 
 
  &lt; option value =“{$ jezyki [jj] .code}”{if $ LANG.lang == $ jezyki [jj] .code} selected {/if}>{$jezyki[jj].name}<  / option&gt; 
 
 {/ section} 
&lt; / select&gt; 
   
 
 

第二个是从谷歌服务器下载的,它是一个谷歌翻译小工具。 我需要的是当用户点击第一个下拉列表中的选项时,脚本应该更改窗口小部件下拉列表的值并触发单击功能以实际翻译页面。

  function changeLang(someth){
 var nLang = someth.value; 
 //$(".goog-te-combo option“)。val(someth.value); 
 // $(”。goog-  TE-组合“)的触发器( '点击')。  
 $(“。goog-te-combo option [value =”+ nLang +“]”)。prop('selected',true).click(); 
} 
   \  n 
 

上面的代码段会更改该值。 我可以看到,例如,选择了英语,但它仍然没有翻译页面

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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);
    }
    
    已采纳该答案
    打赏 评论
  • duandaotui5633 2014-03-14 11:24

    Does this help?

    var $el = $(".goog-te-combo option[value=" + nLang + "]");
    $el.prop('selected',true);
    $el.trigger("click");
    

    More on .trigger().

    打赏 评论
  • douhuang1973 2014-03-14 11:31

    I did the same thing other day. Simple solution since you are using jQuery:

    <select id="selectId"></select>
    
    <script type="text/javascript">
    
    /*for an action event upon a change i did this*/
    $('#first-dropdown-id').change(function(){
         //your code
    
         /*nesting the click trigger function for it to get triggered*/
         $('#widget-dropwdown-id').trigger( "click" ); 
         }
    
    });
    
    
    </script>
    
    打赏 评论

相关推荐 更多相似问题