java - 将条件 spring mvc 参数传递给 javascript url 函数

标签 java javascript spring spring-mvc

我在 spring mvc 应用程序中有一个 javascript 日历控件。当用户单击日历控件中的日期时,用户将被重定向到该日期的详细信息页面。问题是除了 date 之外,我还想在 url 中传递其他参数。如何在 javascript 功能中向 url 添加其他参数?

以下是用于创建仅使用日期作为参数的 url 的 JavaScript:

        <script type="text/javascript">
                    $(document).ready(function() {
                        $("#dayPicker").datepicker({
                            firstDay: 1,
                            dateFormat: "yy-mm-dd",
                            defaultDate: new Date(${calendar.dayMillis}),
                            onSelect: function(dateText, instance) {
                                window.location = "${pageContext.request.contextPath}/calendar?day=" + encodeURIComponent(dateText);
                                }
                        });
                    });
        </script>  

为了便于说明,下面是在 JSP 中其他位置生成 url 的代码,其中包括两个其他可选参数(pideid):

            <c:url var="previousLink" value="/calendar">
                <c:param name="day" value="${calendar.previousDay}" />
                <c:choose>
                    <c:when test="${pid!=null}">
                        <c:param name="pid" value="${pid}" />
                    </c:when>
                    <c:otherwise></c:otherwise>
                </c:choose>
                <c:choose>
                    <c:when test="${eid!=null}">
                        <c:param name="eid" value="${eid}"></c:param>
                    </c:when>
                    <c:otherwise>
                    </c:otherwise>
                </c:choose>
            </c:url>
            <a href="${previousLink}">Previous</a>  

如何更改上面的 javascript,以便它添加 pideid 的参数当且仅当pideid 是否 not null

最佳答案

这可以通过连接由 & 字符分隔的 URL 参数来完成:

var url = "${pageContext.request.contextPath}/calendar?day=" + encodeURIComponent(dateText);

if (pid) {
    url += "&pid=" + encodeURIComponent(pid);
}

if (eid) {
    url += "&eid=" + encodeURIComponent(eid);
}

window.location = url;

关于java - 将条件 spring mvc 参数传递给 javascript url 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22311369/

相关文章:

java - 如何更改对象 ArrayList 中字段的值?

java - Spring MVC - 使用对象请求参数进行表单处理

java - 如何在 Spring Boot 中正确指定数据库模式?

javascript - jquery 数据表自定义过滤器

java - Spring MVC Hibernate Spring Security 错误

java - 为什么我的新鲜字符串前面有空?

执行 Apache HttpClient 时出现 java.lang.InterruptedException

java - Hibernate 标准给出无效结果

javascript - 我如何在 redux-saga 的回调中使用 yield?

javascript - 字母数字正则表达式 javascript