我有一个函数返回一个未定义的变量。我在一些类似的帖子中尝试了所有回复,但没有结果。未定义的Javascript变量 - phcat
identifier_id
已定义,但未定义phcat
。有任何想法吗?
$(function() {
$('body').on('click', '.save_random_profile', function() {
var identifier_id = $(this).attr("id");
$('.created_profile' + identifier_id).html('<img src="img/loader.gif" class="loading" />');
var phcat = document.getElementById('#phcat'+identifier_id).value;
alert(phcat);
$.ajax({
type: "POST",
async: false,
url: "actions/save_random_profile.php",
data: dataString,
cache: false,
success: function(data) {
if (data == 0) {
$('.created_profile' + identifier_id).html('Not Sent!');
} else {
$('.created_profile' + identifier_id).html(data);
}
}
});
return false;
});
});
什么让一个元素的'id'点您可以通过它选择的是'id' - 您已在'identifier_id'中拥有该值。问题本身是因为jQuery对象没有'id'属性。您需要使用'.prop('id')' - 尽管如前所述,这完全是冗余代码。 –
var phcat = $('。phcat#'+ identifier_id).id;到var phcat = $('。phcat#'+ identifier_id).attr('id'); – Roy
更改为'var phcat = $('。phcat#'+ identifier_id).id;'并检查。但是这里的'.id'是什么?我认为它需要'.val()'或'.text()'或'attr('id')'..类似于 –