如何在javascript中为函数提供jsp路径

时间:2017-07-28 07:34:45

标签: javascript java jquery jsp

我有一个jsp页面

  

" addSource.jsp"

出现在eclipse中,如图所示。 enter image description here

现在我的问题是我有一个javascript函数,我必须提供" addSource.jsp"在它中,它可以从按钮点击的当前jsp页面移动到该页面。代码如下

 function addRow(type) {  
         if (type == '') {
                alert("Field is empty");
            }
         else {
         var table = document.getElementById('my_table'); //html table
         var rowCount = table.rows.length; //no. of rows in table
         var columnCount =  table.rows[0].cells.length; //no. of columns in table          
         var row = table.insertRow(rowCount); //insert a row            



         var cell1 = row.insertCell(0); //create a new cell           
         var element1 = document.createElement("input"); //create a new element           
         element1.type = "text"; //set the element type 
         element1.setAttribute('id', 'source'); //set the id attribute 
         element1.setAttribute('name','source'+rowCount);
         element1.setAttribute('value',type);
         cell1.appendChild(element1);

         }


         }

    function generate1() {
             var table = document.getElementById('my_table'); 
             var rowCount = table.rows.length;
             alert(rowCount);
             var f = document.form;
             f.target="";
             f.method="post";
             f.action= 'addSource.jsp?rowCount='+rowCount;



         }  

     <form id="form">

                       <input type="text" id="origin" name="element"/>




                      <table id="my_table">
                                <thead><tr>

                                        <th>Source</th>


                                </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>





                        <input type="button" value="Add row"  name="add" onClick="addRow(document.forms[0].element.value)"  />
                        <input type="submit" value=" Save" />

2 个答案:

答案 0 :(得分:1)

您可以使用location.href在表单中添加id(例如formId)并使用以下内容:

<script type="text/javascript">
    document.getElementById("formId").onclick = function () {
    location.href = "addSource.jsp";
};

答案 1 :(得分:0)

var x=1
    function appendRow(type)
    {
       var d = document.getElementById('div');
       d.innerHTML += "<input type='text' id='source' name='source' value='"+ type +"'><br >";
    }

 <form action="Source" method="post">

                       <input type="text" id="origin" name="origin"/>




                    <div id="div">
 <button onclick ="appendRow(document.forms[0].origin.value)" value="Add Row">Add Row</button>
 </div>          
                        <input type="submit" name="Save" value=" Save" />


                             <hr/>
                    </form>

控制器

@RequestMapping(value = "/Source",params="Save", method = RequestMethod.POST)
    public String source(Model model,HttpServletRequest req,HttpServletResponse res,@RequestParam("source") List<String> ola) {
        System.out.println("size is:" +ola.size());
        for(int i=0; i<ola.size() ;i++) {
            String srh = ola.get(i);
                    System.out.println("srh value is:" +srh);
        }

        return "redirect:/createPoints";
    }