你如何访问 jquery 中的模型属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5021383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:24:05  来源:igfitidea点击:

How do you access a model attribute in jquery?

jqueryspringjspspring-mvc

提问by coder

I'm adding an object to my ModelAndViewin spring and forwarding to my jsp view. I need to access that object in my jquery. Is this possible without first putting the value in a hidden field? How is it done?

ModelAndView在 spring 中添加一个对象并转发到我的 jsp 视图。我需要在我的 jquery 中访问该对象。如果不先将值放在隐藏字段中,这可能吗?它是如何完成的?

回答by Bozho

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

This will resolve the model attribute added by model.addAttribute("modelAttribute", value)

这将解析由添加的模型属性 model.addAttribute("modelAttribute", value)

回答by thil

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.

${modelAttribute}在jsp 的周围添加c:out 。