2017-04-22 59 views
-1

如何刷新您的结果时,让你搜索任何关键字。如果我使用innerHTML,它可以更新我找到的关键字,但是当使用脚本src时我不起作用,它只能工作一次。这是我的编码刷新JavaScript Src

<html> 
    <head> 
    <title>Google Search API</title> 
    </head> 
    <body> 

    <div id="box"> 
    <textarea id="myTextarea"></textarea> 
    <button type="button" id="mySubmit" onclick="myFunction()" >Search</button> 

<p id="demo"></p> 
<div id="content"> 



    </div> 


    <script id="searchme"></script> 
    <script> 
    function myFunction() { 
     var query = document.getElementById("myTextarea").value; 
     var search_query = "https://www.googleapis.com/customsearch/v1?q="+query+"&cx=004123968310343535430%3Aaxml6iel9yo&key=AIzaSyDxPcphnrcN9_dfkRkFTbwkv44m1-HI1Hg&callback=hndlr"; 
    document.getElementById('searchme').src = search_query; 
    } 

    function hndlr(response) { 
    for (var i = 0; i <response.items.length; i++) { 
     var item = response.items[i];  
     document.getElementById("content").innerHTML +="<br>"+"<b>"+ item.title + "</b>"+"<br>" +item.snippet + "<br>"+"<a href='"+item.link+"'>"+item.displayLink+"</a>" ; 
    } 
} 


</script> 


    </body> 
</html> 

回答

0

从按钮中移除侦听器并将其添加到输入中,以便在您输入时立即生效。

<textarea onkeydown="myFunction()"></textarea> 
<button type="button" id="mySubmit" >Search</button> 

虽然,我不建议使用onkeydown,但是这对你来说是有用的。

+0

它不工作,遇到错误 – Rushdiey

+1

显示我的错误 –

+0

遗漏的类型错误:在HTMLTextAreaElement.onkeydown :无法读取性能在myFunction的(22 jquery3.php)空 的“价值”(jquery3.php:8) – Rushdiey