javascript method not found 'Method Name' is undefined
I've implemented a simple JQuery.GetJSON
method on the click of an <img>
tag. The problem is that Internet explorer is throwing exception that methodname is undefined.
Can anybody guide me on this.
HTML:
<div class="itemgenerate">
<img src="/images/generate.png" onclick="sendJSONRequest()" style="cursor: pointer;" />
</div>
<div id="divTarget" class="itemtext">
<p id="pStuff"></p>
</div>
Java Script:
<script language="javascript" type="text/javascript" src="/Scripts/jquery-1.4.1.js" />
<script language="javascript" type="text/javascript">
function sendJSONRequest() {
$.getJSON("/Home/Generate", $('#text1').val(), function (data) {
$('#pStuff').text(data.Stuff);
});
}
</script&g开发者_如何学运维t;
Please if anybody can explain me what is wrong here:
script
tags can't be self-closing. You need a closing script
tag:
<script language="javascript" type="text/javascript" src="/Scripts/jquery-1.4.1.js"></script>
Your current syntax means the first script tag won't be closed and its content will be treated as part of the first. Since the first tag has a src
attribute, its content will be ignored, so your function won't be defined.
It's not the JQuery way to use onclick=
in the tag; one of the main points of using JQuery is to allow you to abstract the script code away from the HTML markup. So you would use something like this instead:
$(document).ready() {
$('#myimage').click(sendJSONRequest);
}
精彩评论