Thymeleaf与JavaScript交互:传递参数给方法
"这是我的前端代码:\n<table>\n\t\t\t\t<tr th:each="product, productStat : ${indexProducts}" th:if="${productStat.index % 3 == 0}">\n\t\t\t\t\t<td th:attr="data-product-name=${product.name}" th:onclick="getDialogData(this.getAttribute('data-product-name'))">\n\t\t\t\t\t\t<el-image class="ProductImage" th:src="${product.imgUrl}" :fit="'cover'"></el-image>\n\t\t\t\t\t\t<span class="ProductName" th:text="${product.name}">product-name</span>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td th:attr="data-product-name=${indexProducts[productStat.index + 1].name}" th:onclick="getDialogData(this.getAttribute('data-product-name'))">\n\t\t\t\t\t\t<el-image class="ProductImage" th:src="${indexProducts[productStat.index + 1].imgUrl}" :fit="'cover'"></el-image>\n\t\t\t\t\t\t<span class="ProductName" th:text="${indexProducts[productStat.index + 1].name}">product-name</span>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td th:attr="data-product-name=${indexProducts[productStat.index + 2].name}" th:onclick="getDialogData(this.getAttribute('data-product-name'))">\n\t\t\t\t\t\t<el-image class="ProductImage" th:src="${indexProducts[productStat.index + 2].imgUrl}" :fit="'cover'"></el-image>\n\t\t\t\t\t\t<span class="ProductName" th:text="${indexProducts[productStat.index + 2].name}">product-name</span>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\t\t\t</table>\n<script>\n\t\t\t\tconst {ref} = Vue\n\t\t\t\tconst products = createApp({\n\t\t\t\t\tdata() {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tdialogVisible: ref(false),\n\t\t\t\t\t\t\tdialogData: ref({})\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\tmethods:{\n\t\t\t\t\t\tgetDialogData(productName){\n\t\t\t\t\t\t\tconst _this = this\n\t\t\t\t\t\t\t_this.dialogVisible = true\n\t\t\t\t\t\t\tconsole.log(productName)\n\t\t\t\t\t\t\taxios({\n\t\t\t\t\t\t\t\turl: "http://localhost:8080/index/getProduct",\n\t\t\t\t\t\t\t\tmethod: "get",\n\t\t\t\t\t\t\t\tparams: {\n\t\t\t\t\t\t\t\t\tname: productName\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}).then(function (resp){\n\t\t\t\t\t\t\t\tconsole.log(resp.data)\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}).use(ElementPlus).mount(".Products")\n\t\t\t</script>\n\我希望在点击td的时候,可以调用getDialogData(name)方法,但我无法通过thymeleaf语法向该方法传参,请问我该怎么做\n\n\thymeleaf语法无法直接在HTML标签中传递参数给JavaScript方法,但你可以通过其他方式实现该功能。以下是两种可能的解决方案:\n\n\1. 使用data-属性传递参数:\n\在HTML标签中添加data-属性,将参数值作为属性值传递给JavaScript方法。在你的代码中,将product.name作为参数传递给getDialogData方法,可以这样修改代码:\n\n```html\n<td th:attr="data-product-name=${product.name}" th:onclick="getDialogData(this.getAttribute('data-product-name'))">\n```\n\n\然后在JavaScript方法中获取data-属性的值,作为参数使用:\n\n```javascript\n\getDialogData(productName){\n\t//...\n}\n```\n\n\2. 使用事件委托:\n\将事件绑定到table元素上,通过事件委托来处理点击事件。在JavaScript中,通过判断点击的元素是td,并获取td内的内容作为参数。在你的代码中,可以这样修改:\n\n```html\n<table th:onclick="handleTableClick($event)">\n```\n\n```javascript\n\handleTableClick(event){\n\tconst target = event.target\n\tif(target.tagName === 'TD'){\n\t\tconst productName = target.querySelector('.ProductName').textContent\n\t\tthis.getDialogData(productName)\n\t}\n}\n```\n\n\这样,当点击td元素时,会调用handleTableClick方法,判断点击的元素是否是td,并获取td内的内容作为参数,然后调用getDialogData方法。\n\n\请根据你的需求选择其中一种方法进行修改。\n

原文地址: http://www.cveoy.top/t/topic/qAii 著作权归作者所有。请勿转载和采集!