Bind click event to 'search' button in Google Custom Search

拟墨画扇 提交于 2020-01-11 13:43:47

问题


I am trying to customize a two column Google custom search. I really liked the ajax over iframe which populates a div (default div#cse). But the problem is it pushes the other contents down breaking the page. So I wanted to hide the contents in div#content when 'search' button is clicked and show again when 'reset button is clicked'. To achieve this i tried the to bind a click event handler to the submit button but it didn't work.

$(document).ready(function(){
    $("input.gsc-search-button[type=submit]").click(function(){
        alert("worked"); 
        //hide div#content
    })      
})

Then I tried following to check if it binds the event. Though it worked its not what I want. The google api do not provide any such callback.

<input id="click" type="button" value="bind event"/>

$(document).ready(function(){
    $("#click").bind('click', function(){
        $("input.gsc-search-button[type=submit]").bind('click', function(){
            alert("worked");
            //hide div#content
        })          
    })
})

Is there any way I can do this?

Thanks.


回答1:


The search box is created with google js api after window.onload, therefore the .bind() fails. Solved the problem with jquery.live().

$("input.gsc-search-button[type=submit]").live('click',showResults);
$(".gsc-clear-button").live('click', hideResults);


来源:https://stackoverflow.com/questions/5560615/bind-click-event-to-search-button-in-google-custom-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!