简体   繁体   English

如何在jquery中访问模型属性?

[英]How do you access a model attribute in jquery?

I'm adding an object to my ModelAndView in spring and forwarding to my jsp view. 我在Spring中向ModelAndView添加了一个对象并转发到我的jsp视图。 I need to access that object in my jquery. 我需要在我的jquery中访问该对象。 Is this possible without first putting the value in a hidden field? 如果没有先将值放在隐藏字段中,这是否可行? How is it done? 怎么做?

<script type="text/javascript">
   var modelAttributeValue = '${modelAttribute}';
</script>

This will resolve the model attribute added by model.addAttribute("modelAttribute", value) 这将解析model.addAttribute("modelAttribute", value)添加的模型属性

probably, you can save the model attribute in a hidden field and access it onload as below. 可能,您可以将模型属性保存在隐藏字段中并按如下方式访问它。

$(document).ready(function(){
  var modelAttr = $("#modelAttr").val();
  alert(modelAttr);
}

input type="hidden" id="modelAttr" name="modelAttr" value="${modelAttribute}"/>

Add c:out around the ${modelAttribute} in the jsp. 在jsp中的${modelAttribute}周围添加c:out。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM