简体   繁体   English

如何将javascript变量传递给servlet

[英]How to pass javascript variable to servlet

I am using selectable jquery in my jsp page.我在我的 jsp 页面中使用可选择的 jquery On submitting the form, the string should pass as parameter to servlet.在提交表单时,字符串应该作为参数传递给 servlet。

$(function() {
    $("#selectable").selectable({
        stop: function() {
            var result = $("#select-result").empty();
            $(".ui-selected", this).each(function() {
                var index = $("#selectable li").index(this);
                result.append(" #" + (index + 1));
            });
        }
    });
});

I want var result as value in <input type="hidden" id="select-result" value=""/>我希望var result作为<input type="hidden" id="select-result" value=""/>

Form -形式 -

<form action="XYZServlet" method="get">
 <input type="text" id="from" name="from">
 <input type="hidden" id="select-result" value="">
 <input type="submit" value="Submit"/>
</form>

How can I do this?我怎样才能做到这一点?

How about:怎么样:

$(function() {
    $("#selectable").selectable({
        stop: function() {
            var result = $("#select-result").empty();
            $(".ui-selected", this).each(function() {
                var index = $("#selectable li").index(this);
                result.append(" #" + (index + 1));
            });
        $("#select-result").val(result);
        }
    });
});

You just need the index , right?你只需要index ,对吧?

$(function() {
    $("#selectable").selectable({
        stop: function() {
            var result = $("#select-result").empty();
            $(".ui-selected", this).each(function() {
                var index = $("#selectable li").index(this);
                result.append(" #" + (index + 1));
                document.getElementById("my").value = index;
            });      
        }
    });
});

You can view the Value as index(selected item) - <input type="text" id="my" value=""/>您可以将查看为索引(所选项目) - <input type="text" id="my" value=""/>

I hope this will work :)我希望这会奏效:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM