如何用javascript编写for循环
How to write the for loop in javascript
从这段代码中,我在get-emplyee.php页面中编写了select查询,我得到了该页面的答案,之后我返回了上一页(home.php)的数据。从该页面中,我在此标记之前为javascript编写了循环class="col-md-3"我想打开for循环并附加class="Under_Manager"的值
从这段代码中,我可以得到这样的错误SyntaxError:预期的表达式,得到了"}"
<script type="text/javascript">
$(document).ready(function(){
$('.getEmployee').click(function(){
var emp_id = $(this).attr("rel");
$.ajax({
type:'POST',
url :"get-employee.php",
data : { emp_id : emp_id},
success: function(data) {
var res=jQuery.parseJSON(data);// convert the json
console.log(res);
if(res['status']=="success"){
var htmlString='';
var htmlString = '<section class="content-header"><h1>Employee</h1></section><!-- Main content --><section class="content"><div class="row"><div class="col-xs-12"><div class="box"><div class="row" style="margin:10px">'+$.each( res['data'], function( key, value ) {+'<div class="col-md-3"><!-- Widget: user widget style 1 --><div class="box box-widget widget-user"><!-- Add the bg color to the header using any of the bg-* classes --><div class="widget-user-header" style="background-color: red;color:#fff"><h3 class="widget-user-username">Soundharajan</h3><h5 class="widget-user-desc">Manager</h5></div><div class="widget-user-image"><img class="img-circle" src="../TV/php/upload/unknown-img.jpg" alt="User Avatar"></div><div class="box-footer"><div class="row"><div class="col-sm-6 border-right"><div class="description-block"><h5 class="description-header">9862626427</h5><span class="description-text">Mobile</span></div><!-- /.description-block --></div><!-- /.col --><div class="col-sm-6"><div class="description-block"><h5 class="description-header">BE</h5><span class="description-text">Education</span></div><!-- /.description-block --></div><!-- /.col --></div><!-- /.row --></div><button type="button" data-loading-text="Loading..." class="btn btn-primary btn-block view_employee" style="background-color: red;color:#fff;border-color:red"> View Task </button></div><!-- /.widget-user --></div></div></div></div>'+});+'</div></section><!-- /.section -->'
$(".Under_Manager").prepend(htmlString);
}else{
console.log('erorr');
}
},
error:function(exception){
alert('Exeption:'+exception);
}
});
});
});
</script>
<!--get-employee.php-->
<?php
session_start();
include('dbconfig.php');
if(empty($_SESSION['email'])){
header('Location:login.php');
}
$userlevel = $_SESSION['is_user_type'];
$emp_id = $_POST['emp_id'];
$sql=mysql_query("SELECT * FROM task_employee WHERE emp_underon ='$emp_id'");
$data = array();
while($row=mysql_fetch_assoc($sql)){
$data[] = $row;
}
$count = sizeof($data);
if($count > 0){
$return=array('status'=>'success','count'=>sizeof($data),'data'=>$data);
echo json_encode($return);
}else{
$return=array('status'=>'error'.mysql_error(),'count'=>sizeof($data),'data'=>$data);
echo json_encode($return);
}
?>
<div class="Under_Manager"></div>
$.each()
不返回回调函数的作用,您需要使用$.map
,然后使用.join()
将所有结果组合回一个字符串。
但一种更简单的方法是将循环从串联中去掉,而是附加到循环中的字符串中。
var htmlString = '<section class="content-header"><h1>Employee</h1></section><!-- Main content --><section class="content"><div class="row"><div class="col-xs-12"><div class="box"><div class="row" style="margin:10px">';
$.each( res['data'], function( key, value ) {
htmlString += '<div class="col-md-3"><!-- Widget: user widget style 1 --><div class="box box-widget widget-user"><!-- Add the bg color to the header using any of the bg-* classes --><div class="widget-user-header" style="background-color: red;color:#fff"><h3 class="widget-user-username">Soundharajan</h3><h5 class="widget-user-desc">Manager</h5></div><div class="widget-user-image"><img class="img-circle" src="../TV/php/upload/unknown-img.jpg" alt="User Avatar"></div><div class="box-footer"><div class="row"><div class="col-sm-6 border-right"><div class="description-block"><h5 class="description-header">9862626427</h5><span class="description-text">Mobile</span></div><!-- /.description-block --></div><!-- /.col --><div class="col-sm-6"><div class="description-block"><h5 class="description-header">BE</h5><span class="description-text">Education</span></div><!-- /.description-block --></div><!-- /.col --></div><!-- /.row --></div><button type="button" data-loading-text="Loading..." class="btn btn-primary btn-block view_employee" style="background-color: red;color:#fff;border-color:red"> View Task </button></div><!-- /.widget-user --></div>';
});
htmlString += '</div></div></div></div></section><!-- /.section -->';
相关文章:
- 为什么JavaScript在for循环为3时向所有4发出警报
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 我的javascript for循环不起作用
- For循环冻结Javascript
- 如何在for循环中添加事件侦听器
- 双“for”循环(循环)
- javascript for循环不起作用
- for循环中的javascript if语句找不到==
- Javascript在for循环中等待处理请求
- For循环在Jquery中只运行一次
- 如何在for循环中使用计数器
- for循环中的JavaScript闭包
- 为什么我们在ES2015中需要一个新的for循环结构,而我们已经有了for、forEach
- For循环在调用时未运行
- 如何使用for循环添加所有按钮'单击事件
- 如何更改在for循环中生成的圆的位置
- 为什么这个For循环会使浏览器实验室崩溃
- 为什么我使用javascript获得了一个无限的for循环
- 在for循环中使用多维数组设置google.maps.Marker图标
- 如何在angularJS中运行for循环而不使用html标记