javascript - Change a form's action dynamically on thymeleaf - Stack Overflow

I currently have a select dropdown box which shows one of several input file browsers (each allows only

I currently have a select dropdown box which shows one of several input file browsers (each allows only one of ".txt", ".cvs" and so on) according to the selected option. I want to change the form's action depending on the selected option so I can handle each case in a different method on the controller's side.

This example works when not using thymeleaf:

$("#myselect").change(function() {
    if($(this).val() == "option1"){
        $("#form").attr("action", "/processopt1");
    }
});

I am trying the following for thymeleaf without success:

$("#myselect").change(function() {
    if($(this).val() == "option1"){
        $("#form").attr("th:action", "@{/processopt1}");
    }
});

Thanks in advance.

I currently have a select dropdown box which shows one of several input file browsers (each allows only one of ".txt", ".cvs" and so on) according to the selected option. I want to change the form's action depending on the selected option so I can handle each case in a different method on the controller's side.

This example works when not using thymeleaf:

$("#myselect").change(function() {
    if($(this).val() == "option1"){
        $("#form").attr("action", "/processopt1");
    }
});

I am trying the following for thymeleaf without success:

$("#myselect").change(function() {
    if($(this).val() == "option1"){
        $("#form").attr("th:action", "@{/processopt1}");
    }
});

Thanks in advance.

Share Improve this question edited Sep 23, 2015 at 11:43 DiegoSahagun asked Sep 23, 2015 at 10:45 DiegoSahagunDiegoSahagun 7581 gold badge11 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You must enable script inlining. For Thymeleaf expressions to be evaluated, you must use the mented out double brackets syntax: /*[[...]]*/

<script th:inline="javascript">
    var action = /*[[@{/processopt1}]]*/ '/processopt1';
    $("#myselect").change(function() {
        if($(this).val() == "option1"){
            $("#form").attr("action", action);
        }
    });
</script>

Because the expression is mented out, templating will still work when staticly displaying the page.

When the expression is evaluated, Thymeleaf will automatically remove any code after the expression.

Data attributes

Another option would be to use data attributes on the items of your select box, which you can also build dynamically with Thymeleaf:

<select id="myselect" th:forEach="item : ${items}>
    <option th:attr="data-action=${item.action}">${item.name}</option> 
</select>

With this option, you can avoid script inlining when you change your script to the following:

$("#myselect").change(function() {
    var action = $("#myselect option:selected").data("action");
    $("#form").attr("action", action);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744275198a4566295.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信