【发布时间】:2014-09-26 06:55:40
【问题描述】:
我需要我之前在 ajax 事件上创建的跨度的 id(和其他属性,例如值)。 有没有办法做到这一点?
这是在 php post 上创建 span 的方式:
echo "<span class='non-skin-symptom-choice disease_option' ".
"onclick='showinfo(".$var[0].");' id=".$var[0].">"
.$var[1]." -- ".number_format($var[3]*100, 2, '.', '')."%</span>";
我想在点击复选框时获取它的 ID。
$(".chb").click(function(){
var id= $(this).attr('id');
var list=[];
$('.disease_option').each(function (){
alert("this.val=="+ $(this).attr("val")); //i need the value here
var str= $(this).attr("value").split(" -- ")[1];
alert(str);
str=str.slice(0,str.length - 1);
if(parseFloat(str) >=support)
list.push(id) //i need the id here
});
复选框不是动态创建的,因此 $(".chb").click(function(){} 有效。
不知何故,$(this).attr("id") 有效,但 $(this).attr("val") 返回未定义...我也尝试过 $(this).attr("value") 但相同结果。 $(this).val 返回空。
【问题讨论】:
-
$(".non-skin-symptom-choice").attr("id");
-
@Tauri28 - 假设只有一个
.non-skin-symptom-choice可能不是这种情况(或者他们不需要那个特定的 id)。 -
你的 .chb 类在哪里?
-
@Kartikeya 这里需要什么事件委托??你读过问题吗?
-
第一:永远不要使用
$(this).attr("id"),只使用this.id(你不必为所有的东西使用jQuery...),第二:在哪里你需要得到id?在您使用onclick或其他地方调用的函数中?第三:显示 html,而不是 php(与在浏览器中运行的 JavaScript 无关*)。 (*我知道服务器端 JavaScript,你没有使用它。)
标签: javascript php jquery ajax