如何将变量放在内联 javascript [[]] api 路径中 [英] How can I put the variable in the inline javascript [[]] api path

查看:30
本文介绍了如何将变量放在内联 javascript [[]] api 路径中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我需要在javasctipt中补上url.但我不知道如何将变量放入 th:inline="javascript";.我的代码如下:

I have a question, I need make up the url in the javasctipt. But I don't known how to put the variable in the th:inline="javascript" . My code below:

 <script th:inline="javascript">
 $(function() {
    $('#querySubmit').click(querySubmitClickAction);
    querySubmit.addEventListener('click', querySubmitClickAction);
    function querySubmitClickAction(e) {
        
        var theSize = 10;
        var name = $(this).val();
        $.ajax({
            url: /* 
[[@{/registeredUserList(type=0,userName=defaultName,page=0,size=10)}]]*/ 'dummy',
            type: 'POST',
            success: function (data) {
                $(".table_content").html(data);
            }
        })
     }
  });

如何使用 [[]] 中的变量来组成 url.

How can I make up the url using the variable in the [[]].

 url: /*[[@{/registeredUserList(type=0,userName=name,page=0,size=theSize)}]]*/ 'will show error',

它会显示错误.如何使用放在 [[]] 中的 js 变量.

It's will show error. How can I use the js variable put in the [[]].

非常感谢.

推荐答案

你可以这样做:

<script th:inline="javascript">
    /*<![CDATA[*/

    // JavaScript variable
    var theSize = 10;

    // URL from Thymeleaf, note the round brackets to avoid that / becomes \/
    var theUrl = "[(@{/users/})]";

    // Create an URL object resolving the relative url
    var url = new URL(theUrl, document.location);

    // Update the query parameters of the URL with the JavaScript variables
    url.searchParams.append("size", theSize);

    console.log(url);

    /*]]>*/
</script>

在 JavaScript 控制台中,打印:

In the JavaScript console, this prints:

http://localhost:3000/users/?size=10

http://localhost:3000/users/?size=10

这篇关于如何将变量放在内联 javascript [[]] api 路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆