我是Javascript和Ajax的新手,并试图弄清楚为什么我的代码无效。
<script type="text/javascript">
$(document).ready(function() {
$('.cadlib').bind('click', function () {
$.get("/models/"+this.id+"/viewmodelinfo/", function(data) {
alert ("yo"); // This line bellow doesn't execute
if (data.fact_type=="T") {
guess_result="This fact is true! " + data.fact_note;
} else {
alert ("yo");
guess_result="This fact is false! " + data.fact_note;
}
$('#result').replaceWith("guess_result");
});
});
});
</script>
这是我的HTML
<a href = "#mySymbolModal" class = "cadlib" id = "{{symbol.id}}" data-toggle="modal" style = "text-align:center; color: #FF761A;" href="/models/{{symbol.id}}/view_model/"><span id = "modelname"> {{symbol.modelname.upper}}</span> - <span id = "format">{{ symbol.format }}</span><span id = "adder" style="visibility:hidden;">{{symbol.adder}}</span><span id = "adderid" style="visibility:hidden;"> {{symbol.adder.id}}</span><span id = "symbolobject" style="visibility:hidden;">{{symbol}}</span></a>
我认为注释行有问题。我的警报没有实例化。
是否有一些我错过的会导致警报不起作用的事情?
答案 0 :(得分:1)
我猜它的'this.id',尝试在$ .get之前使用alert(this.id)查看它的生成(或不生成)。
我做了类似的事,应该给你一个想法
$(".productpicturedelete").on("click", "input[type=button]", function(event){
var $buttonid = $(this).attr("id");
答案 1 :(得分:0)
网址字段中缺少数据源文件名:
$。get(“/ models /”+ this.id +“/ viewmodelinfo /&lt; data source file name&gt;”,function(data){..});
答案 2 :(得分:0)
<script type="text/javascript">
$(document).ready(function() {
$('.cadlib').bind('click', function () {
data_id = $(this).attr('id'); //add this
$.get("/models/" + data_id + "/viewmodelinfo/", function(data) {
alert ("yo"); // This line bellow doesn't execute
if (data.fact_type=="T") {
guess_result="This fact is true! " + data.fact_note;
} else {
alert ("yo");
guess_result="This fact is false! " + data.fact_note;
}
$('#result').replaceWith("guess_result");
});
});
});
</script>
答案 3 :(得分:0)
我几乎可以肯定this.id在此上下文中不存在
$.get("/models/"+this.id+"/viewmodelinfo/", function(data)
this.id支持什么?你能解释一下吗