如何在html中获得controller中传过来的数据

有时候我们需要从html中获得controller传过来的数据参数一般都是可以通过ModelAndview
但是这种方法好像适用于jsp
所以直接上代码

	@GetMapping("/add")
			@RequiresPermissions("department:villagePromise:add")
			String add(Model model){
		//获取用户对应的villageId
		int villageId = userService.getVillageByUserId(getUser().getUserId());
		model.addAttribute("villageId", villageId);
		//获取用户对应的parentid
		int parentId = userService.getParentByUserId(getUser().getUserId());
		model.addAttribute("parentId", parentId);
		return "department/villagePromise/add";
		
		}

代码controller中这样写在html中在中这样写

<head th:replace="includeV2::header(_, ~{::links})">
	<th:block th:fragment="links">
		<link th:href="@{/css/plugins/datapicker/datepicker3.css}" rel="stylesheet">
	</th:block>
	<script type="text/javascript">
		
		var items = '[[${villageId}]]';
		var parentId = '[[${parentId}]]';
	</script>
	</head>

这样就可以获得传过来的数据了
在div中这样写就可以获得了

<div class="form-group" style="display: none">	
								<label class="col-sm-3 control-label">上一级:</label>
								<div class="col-sm-8">
									<input id="parentId" name="parentId" class="form-control" type="text" th:value= "parentId"> 
								</div>
							</div>
<div id="villageDiv" class="form-group" style="display: none"  >	
								<label class="col-sm-3 control-label">所属村社(村庄):</label>
								<div class="col-sm-8">
									<input id="townId" name="townId" type="hidden"> 
									<input id="town" name="town" class="form-control" type="text"
										style="cursor: pointer;" onclick="openDept()"
										readonly="readonly" placeholder="所属村/社区">
								</div>
							</div>
							

在js中这样写因为有需要villageId所以js要这样写

$().ready(function() {
$("#villageId").val(items);
}

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值