Ajax轮询 select循环输出
弹出层
<include file="Pub:header"/> <style> .del{color:red} .addname{color:#337ab7} </style> <div class="col-lg-12"> <div class="panel panel-default"> <!-- /.panel-heading --> <div class="panel-body"> <div class="table-responsive"> <table id="table" class="table table-striped table-bordered table-hover"> <thead> <tr> <th>款项名目</th> <th>操作</th> </tr> </thead> <tbody> <volist name="list" id="vo"> <tr class="option"> <td>{$vo.name}</td> <td><a class="del" role="button" onclick="del({$vo.id})">删除</a></td> </tr> </volist> </tbody> <tr><td colspan="2" ><a class="addname" href="javacript:void(0);">添加...</a></td> </tr> </table> </div> <!-- /.table-responsive --> </div> <!-- /.panel-body --> </div> </div> <include file="Pub:footer"/> <script> <!-- 添加款项名目 --> $(".addname").on("click",function(){ layer.prompt({title: '添加款项名目', formType: 0}, function(text, index){ layer.close(index); $.post("{:U('Contracts/setmoneyname')}",{"name":text},function(json){ layer.close(index); if(json.status = 1){ layer.msg('您成功添加了款项名目:'+ text ); window.location.reload(); }else{ layer.msg(json.info); } }); }); }); <!-- 删除款项名目 --> function del(id){ $option = $(this).parents('.option'); var index = layer.load(1, { shade: [0.1,'#fff'] //0.1透明度的白色背景 }); $.post("{:U('Contracts/delmoneyname')}",{"id":id},function(json){ layer.close(index); if(json.status = 1){ layer.msg('您成功删除了款项名目' ); $option.remove(); window.location.reload(); //刷新窗口 }else{ layer.msg(json.info); } }); } //表单提交 _close = parent.layer.getFrameIndex(window.name) //关闭iframe $("#frameclose").on('click',function(){ parent.layer.close(_close); }); $("[name='is_public']").on('change',function(){ $is_public = $(this).val(); if($is_public == 1 || $is_public == 2){ $("[name='customer_id']").parent().parent().slideUp(); }else{ $("[name='customer_id']").parent().parent().slideDown(); } }); </script>
父页面轮询输出
<!-- 添加款项名目 --> $("body").on("change",".addname",function(){ if($("option:selected",this).val()== 1000){ layer.open({ type: 2, title: '编辑款项名目', skin: 'layui-layer-rim', //加上边框 area: ['30%', '70%'], //宽高 content: '{:U("Contracts/setmoneyname")}' }); } }); <!-- 实时刷新款项名目 --> $(window).on('load', function () { window.setInterval(function(){$.ajax({ //5秒刷新一次 type: "POST", url: '/index.php?m=User&c=Contracts&a=moneylist', success: function(data){ var rest = data.info; var string = ''; $addname = $("body").find(".addname"); for (var i = 0; i < rest.length; i++) { var oname = rest[i].name; var oid = rest[i].id; string +="<option value='"+oid+"'>"+oname+"</option>"; //循环每一条 } string +="<option value='1000' style='color:#337ab7'>编辑款项名目...</option>"; //将循环的每条加在上一条后面 $addname.each(function(){ $addname.html(string); //输出所有option 不好的地方是每次刷新,如果选中了就会被刷新掉selected }); } })},5000); }); /* $(window).on('load', function () { $("body").find(".addname").each(function(){ var nameid = $(".addname").find("option:selected").val(); alert(nameid); window.setInterval(function(){$.ajax({ type: "POST", url: '/index.php?m=User&c=Contracts&a=moneylist', success: function(data){ var rest = data.info; var string = ''; $addname = $("body").find(".addname"); for (var i = 0; i < rest.length; i++) { var oname = rest[i].name; var oid = rest[i].id; string +="<option value='"+oid+"'>"+oname+"</option>"; } string +="<option value='1000' style='color:#337ab7'>编辑款项名目...</option>"; $addname.each(function(){ $addname.html(string); $addname.find("option[value='"+nameid+"']").attr("selected","selected"); }); } })},5000); }); });*/
信息创造价值, 知识就是力量。