Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用document.form.action提交前检查值_Javascript_Jquery_Spring Mvc_Pdf_Submit - Fatal编程技术网

Javascript 使用document.form.action提交前检查值

Javascript 使用document.form.action提交前检查值,javascript,jquery,spring-mvc,pdf,submit,Javascript,Jquery,Spring Mvc,Pdf,Submit,在我的web应用程序中,我有一个带有两个的表单。 一个代表月份,另一个代表年份 <form id="reportform" target="_blank"> <table cellpadding="3" cellspacing="14"> <thead> <th class="report" colspan="2"><spring:message code="label.month"/></th&g

在我的web应用程序中,我有一个带有两个
的表单。 一个代表月份,另一个代表年份

<form id="reportform" target="_blank">
    <table cellpadding="3" cellspacing="14">
    <thead>
        <th class="report" colspan="2"><spring:message code="label.month"/></th>                        
        <th class="report" colspan="2"><spring:message code="label.year" /></th>
        <th colspan="2"></th>
    </thead>
    <tbody>
    <tr>
        <td>
            <input name="centerId" value="${center.centerId}" type="hidden">
        </td>
        <td class="date">
            <select class="select-input-month-report" id="select-input-month" name="month">
                <option selected="selected" value="0"><spring:message code="label.select" /></option>
                <option value="1"><spring:message code="label.january" /></option>
                <option value="2"><spring:message code="label.february" /></option>
                <option value="3"><spring:message code="label.march" /></option>
                <option value="4"><spring:message code="label.april" /></option>
                <option value="5"><spring:message code="label.may" /></option>
                <option value="6"><spring:message code="label.june" /></option>
                <option value="7"><spring:message code="label.july" /></option>
                <option value="8"><spring:message code="label.august" /></option>
                <option value="9"><spring:message code="label.september" /></option>
                <option value="10"><spring:message code="label.october" /></option>
                <option value="11"><spring:message code="label.november" /></option>
                <option value="12"><spring:message code="label.december" /></option>
            </select>
        </td>
        <td></td>
        <td class="date">
            <select class="select-input-year-report" id="select-input-year" name="year">
                <option value="0" selected="selected"><spring:message code="label.select" /></option>
                <option value="2013">2013</option>
                <option value="2014">2014</option>
            </select>
        </td>
        <td align="right">
            <input id="go" class="go" type="submit" value="Report">
        </td>
    </tr>
    </tbody>
    </table>
</form>
我要求这样的服务:

$(function(){
    $('form').submit(function(){
        var zeros = checkDate();
        if(zeros == '0'){
            document.reportform.action = "/report.pdf";
        }
    });
});

function checkDate(){
    var zeros = 0;
    if(document.getElementById("select-input-month").value == 0){
        zeros++;
    }
    if(document.getElementById("select-input-year").value == 0){
        zeros++;
    }
    return zeros;
}
我们将不胜感激

更新:
我收到Javascript错误:
document.reportform.action=“/report.pdf”未定义。

使用JQuery它非常简单:

    $(function(){
        $('form').submit(function(e){
        e.preventDefault();
        if($("#select-input-month").val()=='0' || $("#select-input-year").val()=='0')
        {
            document.reportform.action = "/report.pdf";
        }
        });
    });

仅使用javascript运行验证不是一个好主意。您也应该在控制器中执行此操作。您已经在使用jQuery,那么为什么不使用
$(“#select input month”).val()
?您实际上不会说它有什么问题。你能解释一下它在做什么吗?@DhavalMarthak你说得对,谢谢。看起来很接近,但我发现一个错误“get”方法不受支持。我之所以定义为POST,是因为我正在向后端发送数据。有什么想法吗?对,我忘了改变方法。但是现在我在构造ReportRequest对象时遇到了一个问题。假设参数是在主体响应中发送的,而不是在URL中,对吗?我在html中没有看到任何模型属性。可能您可以使用HTTP请求本身,无需这样做。当我发送表单时,请求主体有3个参数。SpringMVC直接创建了我的ReportRequest对象(
@ModelAttribute ReportRequest请求
)。我正在检查您的解决方案,该解决方案可以工作,但并不比使用所有SpringMVC电源更聪明。
    $(function(){
        $('form').submit(function(e){
        e.preventDefault();
        if($("#select-input-month").val()=='0' || $("#select-input-year").val()=='0')
        {
            document.reportform.action = "/report.pdf";
        }
        });
    });
<form id="reportform" target="_blank" method="post">

    $(function(){
        $('form').submit(function(e){
            var zeros = checkDate();
            if(zeros == '0'){
                $(this).attr("action","/report.pdf");
            }
            else{
                e.preventDefault();
                alert('Select month and year');
            }
        });
    });
@Controller
public class ReportController {

    @RequestMapping(value="/report.pdf", method=RequestMethod.POST)
    public ModelAndView getPdfReport(@ModelAttribute ReportRequest request, HttpServletRequest httpRequest) throws DAOException {        

        String month = httpRequest.getParameter("month");
        String year= httpRequest.getParameter("year");
        ....
        return mav;
    }
}