我有一个JSON api返回响应,如下所示:
{
“formId”:2211,
“formName”:“测试表名称1”,
“fileCount”:0,
“createdOnDate”:“2012-10-22T13:31:00”,
“modifiedDate”:“2012-10-22T13:31:00”
},
{
“formId”:2212,
“formName”:“测试表名称2”,
“fileCount”:2,
“createdOnDate”:“2012-10-22T13:31:00”,
“modifiedDate”:“2012-10-22T13:31:00”
},
这在前端显示为
下拉列表<div class="select with-hover"><ul>
<li><a href="#" data-key="2211" data-value="Test Form Name1">Test Form Name1</a></li>
<li><a href="#" data-key="2212" data-value="Test Form Name2">Test Form Name2</a></li>
<li><a href="#" data-key="2213" data-value="Test Form Name1">Test Form Name3</a></li>
当filecount为0时,如何在单击链接时显示警告消息?
答案 0 :(得分:0)
将fileCount数据属性添加到<a>
元素。我完全猜测你提供的其他代码是否有助于你提供:
$.ajax('your/url',{your:data},function(response) {
$(div).append($('<a href="#"></a>').data({key:response.key, value:response.value, filecount: response.fileCount}).text(response.formName));
});
然后你需要一个点击处理程序:
$(function(){
$(div).on('click','a', function(e) {
if ($(this).data('filecount') == 0) {
alert('filecount is 0');
}
e.stopPropagation(); // stop click events on parent elements
return false; // don't actually go to the href
});
});