【问题标题】:How can i use for loop in javascript using thymeleaf?如何使用 thymeleaf 在 javascript 中使用 for 循环?
【发布时间】:2023-03-26 01:16:01
【问题描述】:

我正在使用 thymeleaf,所以当我运行这个应用程序时,它给了我一个错误 (for(int i=0;i

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handing Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
        <script type="text/javascript" src="jquery-1.11.3.js"></script>

</head>
<body>
    <h1>Result</h1>
    <p th:text="'columns_number: ' + ${db.columns_number}" />
    <h3>Création de la base de données</h3>
    <p>Table_name: <input type="text" th:field="${db.table_Name}" /></p>
<table id='tablona' border='1px'>
        <tr>
        <th>field</th>
                <th>Size</th>
                <th>Type</th>
                <th>null</th>
                </tr>


</table>

<script th:inline="javascript">
    /*<![CDATA[*/
    $( document ).ready(function() {

        for(int i=0;i<[[${T(Integer).parseInt(db.columns_number)}]];i++)
      {
        $('<tr>'+
        '<td><input id="field" type="text" name="field'+i+'"  maxlength="255"  required="required"/></td>'+
        '<td><input id="Size" type="text"  name="Size'+i+'"  maxlength="255" required="required"/></td>'+
        '<td><SELECT id="Type" name="Type'+i+'">'+
'<OPTION VALUE="varchar">varchar</OPTION>'+
'<OPTION VALUE="int">int</OPTION>'+
'<OPTION VALUE="text">long</OPTION>'+
'<OPTION VALUE="float">float</OPTION>'+
'<OPTION VALUE="double">double</OPTION>'+
'<OPTION VALUE="Date">Date</OPTION>'+
'<OPTION VALUE="Time">Time</OPTION>'+
'<OPTION VALUE="Year">Year</OPTION>'+
'<OPTION VALUE="Real">Real</OPTION>'+
'<OPTION VALUE="Boolean">Boolean</OPTION>'+
'<OPTION VALUE="longText">longText</OPTION>'+
'<OPTION VALUE="Binary">Binary</OPTION>'+
'</SELECT></td>'+
'<td><SELECT id="null" name="nullabilite'+i+'">'+
'<OPTION VALUE="null">null</OPTION>'+
'<OPTION VALUE="not_null">not_null</OPTION>'+
'</SELECT></td>'+
    '</tr>').appendTo($("#tablona")).html()

    }
    });
    /*]]>*/
</script>
      <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>    

</body>
</html> 

【问题讨论】:

    标签: javascript thymeleaf


    【解决方案1】:
     <table border='1px'>
        <thead>
            <th>field</th>
            <th>Size</th>
            <th>Type</th>
            <th>null</th>
        </thead>
        <tbody> </tbody>
    </table>
    <script th:inline="javascript">
        /*<![CDATA[*/
        $(document).ready(function () {
            for (var i = 0; i < 10; i++) {
                var row=[],r=0;
                row[r]="<tr>";
                row[++r]='<td>';
                row[++r]='<input id="field" type="text" name="field"';
                row[++r]=i;
                row[++r]='maxlength="255"  required="required"/>';
                row[++r]='</td><td>';
                row[++r]='<input id="Size" type="text"  name="Size"';
                row[++r]=i;
                row[++r]= 'maxlength="255" required="required"/>';
                row[++r]= '</td>';
                /*
                * this more readable
                * other td
                *
                */
                row[++r]='</tr>';
               $("tbody").append(row.join(""));
            }
        });
        /*]]>*/
    </script>
    

    【讨论】:

      【解决方案2】:

      你应该把你的脚本包装在这个结构中:

      <script th:inline="javascript">
          /*<![CDATA[*/
          $( document ).ready(function() {
              for(i=0;i<10;i++) {
                  ...
              }
          });
          /*]]>*/
      </script>
      

      编辑:

      不要忘记将你的 javascript 和其他静态文件存储在你的 spring-boot 项目的 /src/main/webapp 文件夹中

      EDIT2:

      您可以使用 thymeleaf 直接执行您的脚本:

      <tr th:each="i : ${#numbers.sequence( 1, db.columns_number)}">
          <td><input id="field" th:name="${'field'+i}" maxlength="255"
              required="required" type="text" /></td>
          <td><input id="Size" th:name="${'Size'+i}" maxlength="255"
              required="required" type="text" /></td>
          <td><select id="Type" th:name="${'Type'+i}">
                   ...
              </select></td>
          <td><select id="null" th:name="${'nullabilite'+i}">
                  <option value="null">null</option>
                  <option value="not_null">not_null</option>
          </select></td>
      </tr>
      

      【讨论】:

      • 我还有一个问题,而不是 10,我想输入我的表单,即我应该写的列数(int i=0;i
      • 你应该把它包裹在[[ ]]:for(int i=0;i&lt; [[${db.columns_number}]] ;i++)
      • 你必须更详细。控制台错误?您是否检查给定结果以检查返回值?
      • 很抱歉,但所有这些技术对我来说都是新的,所以我努力理解。控制台没有错误,它只是给了我一个没有行的表,就像他不知道属性 db.columns_number
      • &lt;/body&gt;之前用这个检查返回值:&lt;p th:text="${db.columns_number}"&gt;Default&lt;/p&gt;显示的是什么?
      【解决方案3】:

      thymeleaf 当前脚本模式是 javascript (th:inline="javascript")dart (th:inline="dart")

      使用下面的 sn-p 代替 &lt;script type="text/javascript"&gt;

      <script th:inline="javascript">
      /*<![CDATA[*/
      ...
      
         //your code here
      
      ...
      /*]]>*/
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-12
        • 1970-01-01
        相关资源
        最近更新 更多