2016-10-01 5 views
-6

Wie der Titel sagt, funktioniert der folgende Code nicht und ich weiß nicht warum.Javascript-Code funktioniert nicht. Weiß jemand warum?

Html:

<html> 
    <head> 

    </head> 
    <body> 
    <form> 
     <input type="textbox" class="probe"></input> 
    <button class="probar">h</button> 
    </form> 
    <p id="pu">h<p> 
    </body> 
</html> 

JS:

$(document).ready(function(){ 
//Global variables 
    var poste = ""; 
    var wSearch= ""; 


$("probar").on("click",function(){ 
//Doing a JSON request to wikipedia api 
$.getJSON("https://en.wikipedia.org/w/api.php?action=opensearch&format=jsonfm&search=America&namespace=0&limit=10&redirects=resolve&format=json&callback=?", function(data) { 
//Triying to get the data returned. It doesn't works. 
    console.log(data); 
}); 
    }); 

}); 

Also es soll Funktion ist, dass, wenn Sie die "probar" klicken sie für eine JSON aus der Wikipedia-API fragt und es setzt sie in die Konsole. Das ist alles. Und es funktioniert nicht.

Kann mir bitte jemand helfen?

Antwort

-1
<!DOCTYPE html> 
<html> 
<head> 
    <title>form</title> 
</head> 
<body> 
    <input id="txtQuery" type="text" /> 
    <button id="btnRequest">Request</button> 

    <pre id="txtResponse"><pre> 
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script> 
    <script> 
    $(document).ready(function(){ 

      $("#btnRequest").click(function(){ 
       //Doing a JSON request to wikipedia api 
       $.getJSON("https://en.wikipedia.org/w/api.php?action=opensearch&format=jsonfm&search="+ $("#txtQuery").val() + "&namespace=0&limit=10&redirects=resolve&format=json&callback=?", function(data) { 
        $("#txtResponse").html(JSON.stringify(data,null,4)); 
       }); 
      }); 
    }); 
    </script> 
</body> 
</html> 
+0

Nun, es funktioniert! Ich weiß nicht warum, aber es tut, danke. –

+0

bitte nicht abstimmen antwort .. sagen sie mir direkt, wenn es ein problem in der antwort gibt. – asissuthar

1

Sie haben nichts unternommen, um JavaScript mit HTML zu verknüpfen. Sie benötigen ein <script> Element dafür:

<script src="foo.js"></script> 

Sobald Sie tun, dass Sie einen Referenzfehler in der Konsole Ihres Browsers Entwickler-Tools sehen werden, weil $ nicht definiert ist. Sie benötigen ein anderes Skriptelement, um die jQuery-Bibliothek zu laden, von der Sie abhängig sind.

Dann müssen Sie über selectors lernen.

probar ist eine Typenauswahl und entspricht <probar> Elementen, die in HTML nicht zulässig sind.

Eine Klasse Selektor beginnt mit einem .

+0

Danke, aber im wiht Code Stift io arbeiten, Sie don‘ Ich muss diese t machen Lieder. Aber ich habe den Klassenwähler verpasst, danke dafür. –

0

HTML

<!DOCTYPE html/> 
<html> 
<head> 
<title></title 
<script src="javascript_file.js"></script> 
</head> 
<body> 
<form> 
<input type="textbox" class="probe"></input> 
<button class="probar">h</button> 
</form> 
<p id="pu">h<p> 
</body> 
</html> 

Javascript

$(document).ready(function(){ 
//Global variables 
var poste = ""; 
var wSearch= ""; 


$(".probar").on("click",function(){ // add dot begining class selector 
//Doing a JSON request to wikipedia api 
$.getJSON("https://en.wikipedia.org/w/api.php? action=opensearch&format=jsonfm&search=America&namespace=0&limit=10&redirects=resolve&format=json&callback=?", function(data) { 
//Triying to get the data returned. It doesn't works. 
console.log(data); 
}); 
}); 

}); 
+0

Eingabefeld Textbox? – derloopkat

+0

für wählen? nicht notwendig –

Verwandte Themen