jQuery添加到JSP页面

我有一个jQuery代码,我在互联网上找到,我想将它集成到我的jsp页面,我使用Spring表单标签。

这是jQuery代码:

(function ($) { //тут превращаем select в input var id = "test", $id = $('#' + id), choices = $id.find('option').map(function (n, e) { var $e = $(e); return { id: $e.val(), text: $e.text() }; }), width = $id.width(), realClass = $id.get(0).className, realId = $id.get(0).id, $input = $('',{width: width}); $id.after($input); $id.hide(); $id.find('option').remove(); //превратили $input.select2({ query: function (query) { var data = {}, i; data.results = []; // подтставим то что искали if (query.term !== "") { data.results.push({ id: query.term, text: query.term }); } // добавим остальное for (i = 0; i < choices.length; i++) { if (choices[i].text.match(query.term) || choices[i].id.match(query.term)) data.results.push(choices[i]); } query.callback(data); } }).on('change',function() { var value=$input.val(); $id.empty(); $id.append($('').val(value)) $id.val(value); } ); })(jQuery); 

jQuery的CSS文件 – 我把它命名为test.css并将其应用到我的jsp页面:

 #test { width: 300px; } 

我的jsp页面

     Страница выборки        $(document).ready(function() { $("#parctdate, #chldAdmitDate, #chldSchlDate, #name, #type, #daySchdl, #workSchdl, #rotation, #numbch, #chUnder3, #chUpper3, #chGoSchool, #chAdmitted").mouseenter(function() { $(this).css("background-color", "gainsboro"); }); $("#parctdate, #chldAdmitDate, #chldSchlDate, #name, #type, #daySchdl, #workSchdl, #rotation, #numbch, #chUnder3, #chUpper3, #chGoSchool, #chAdmitted").mouseleave(function() { $(this).css("background-color", "white"); }); var enabledDays = ["6-1-2013", "7-1-2013", "8-1-2013", "9-1-2013", "10-1-2013", "11-1-2013"]; function nationalDays(date) { var m = date.getMonth(), d = date.getDate(), y = date.getFullYear(); for (i = 0; i  date) { return [true]; } } return [false]; } $(function(){ $.datepicker.setDefaults($.extend($.datepicker.regional["ru"])); $("#datepicker1, #datepicker2, #datepicker3").datepicker({dateFormat: "yy-mm-dd", duration: "normal", numberOfMonths: [ 1, 2 ], constrainInput: true, beforeShowDay: nationalDays}); }); });     

-

我想将我的jQuery脚本与我的jsp和Spring表单标签集成。

对不起,我是jQuery的新手。

谢谢

与任何JavaScript一样,jQuery被添加到JSP页面的标记中的标记中。 您可以添加所有代码,也可以只添加指向包含jQuery的.js文件的链接,例如:

  

完成后,您现在希望在HTML标记中利用jQuery,就像对任何HTML页面一样。 也就是说,在您的情况下,您不必带走弹簧标签。 让它通过${listOfInstitutionsNames}生成select / options,只需将class="testclass"添加到spring form标签中,如下所示:

  

在浏览器上呈现表单时,Spring将在生成的HTML中包含值为testclass的class属性。

希望有所帮助,祝你好运。

对于Dynamic Web Project(使用MVC模型设计)

在头部添加如下:

  

我将jQuery.js保存在WebContent文件夹中(使用jsp页面)。

如果你的意思是你想将Java端信息绑定到JS var ,你可以像我一样:

  1. 在Java端,使用Google的Gson将Java对象编码为Json字符串。

  2. 在Java端,使用org.apache.commons.lang.StringEscapeUtils.escapeJavaScript(String)使Json字符串作为JavaScript进行转义。

  3. 在JSP方面,做这样的事情: