【问题标题】:How to pass a javascript return value to spring path variable如何将javascript返回值传递给spring路径变量
【发布时间】:2020-07-15 23:48:42
【问题描述】:

我的目标是获取 javascript 函数的返回值,然后作为 spring 路径变量传递

我试着做这个代码:

  • 下拉列表,填充用户想要获取的数据

                <label for="cust">Select a customer</label> <select
                    id="cust" class="form-control">
                    <option  th:each="customer: ${customers}"
                        th:value="${customer.id}" th:utext="${customer.name}" />
                </select>
    
  • 脚本,获取选中的客户

    <script>
    function getSelected() {
        var cust_id = document.getElementById("cust").value;
        return cust_id;
    }</script>
    
  • 按钮,这是我迷路的地方。如何使用 th:href 将此 cust_id 发送到我的控制器?

                    <input type="button" class="button form-control btn-primary"
                    th:value="Next" onclick="getSelected()" th:href="@{/locate/{id}(id=${cust_id})}">
    

我的问题是如何将 cust_id 作为参数传递? 如果有最简单的方法,请告诉我如何

【问题讨论】:

    标签: javascript spring thymeleaf dropdown path-variables


    【解决方案1】:

    我也遇到过类似的情况。这就是我实现它的方式:

    $('#cust').bind('change',function() {
        var custid = $(this).children("option:selected").val();
        alert("You have selected the Customer with id - " + custid);
        window.location = '/myAppURL?pareaName='+ $(this).val();
        });
    

    这意味着从选择(选项)中选择一个值,我必须将它与更改事件绑定(我使用 JQuery)。不确定它是否符合您的“确切”要求,但希望您明白这一点。

    【讨论】:

    • 如果它对您有用,请您接受答案以表示感谢吗?
    • 是的,当然,你的剪报帮助了我。我保留了我的脚本的分配变量 cust_id,因为它能够检索用户选择,然后我使用了你的 sn-p 的 windows.location 对象,并且我已经连接到我的 cust_id 的结果。效果很好,谢谢 Ajay。
    猜你喜欢
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多