有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javascript如何根据条件传递数据?

我有一个页面,可以选择通过电子邮件发送单个文件,也可以选择勾选几个文件并通过电子邮件发送。他们都用各自的JavaScript函数调用了这个页面email.jsp

电子邮件。jsp:

 String files=null;
        String url=null;
        String id=null;
        String hash=null;
        String[] array=null;
        String[] split=null;


        if(multiemail.equals("no")) {

           //get parameters from email()


           files= request.getParameter("filename");
            url = request.getParameter("link");
            id = request.getParameter("id");
            hash = request.getParameter("hash");

       }else{

            split = request.getParameter("link").split(",",0);
            array = request.getParameter("arrayList").split(",",0);

        }

这意味着,如果是一封电子邮件,则获取这四个参数;如果是多封电子邮件,则获取这两个参数。到目前为止还可以。现在,一旦我得到属性,我想把参数传递给sendemail.jsp来处理数据

所以我的发送按钮将调用sendmessage()函数:

$.ajax({

               url: 'sendemail.jsp',
               type: 'POST',
               data: {
                   recipient: recipient,
                   subject: subject,
                   content: content,
                   id:"<%=id%>",
                   hash:"<%=hash%>"


               },

               success: function (data) {
                   alert("Successfully initiated email to queue");
               },
               error: function (request, error) {
                   alert("Request: " + JSON.stringify(error));
               }
           });

所以在这个ajax中,它基本上是基于一封电子邮件来传递数据。当然可以。但是,如果我发送多封电子邮件并单击同一个发送按钮,它会将相同的数据传递到下一页,但不需要idhash,正如你从我的电子邮件中看到的那样。jsp

现在,我不知道是否有解决办法。有没有一种方法可以根据条件传递数据


共 (1) 个答案

  1. # 1 楼答案

    当然,有办法做到这一点

    只需在ajax调用之前创建一个条件,并在if条件的{}括号内,使用参数发出ajax请求。如果有多个If条件,那么可能需要编写多个ajax请求来满足该条件

    如果我要这么做,这就是我的javascript代码

           files= request.getParameter("filename");
            url = request.getParameter("link");
            id = request.getParameter("id");
            hash = request.getParameter("hash");
    
    
    //  javascript code 
    
    if(files != '' && url != '' && id != '' && hash != '' ) {
    
        $.ajax({
    
            // ajax code
    
        });
    
    } else if (id != '' || hash != '' ) {
    
        $.ajax({
    
            // ajax code
    
        });
    
    }