小编典典

用ajax发送参数

jsp

我正在尝试使用ajax将从jsp的表中获取的参数发送到其他JSP。我正在使用followinf函数将所有值发送到JSP:ajaxForm,但是我不知道为什么每次运行都发送失败:

这是javascript函数:

function editarow() {
    var xhr = getXhr();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            selects = xhr.responseText;
            // On se sert de innerHTML pour rajouter les options a la liste
            document.getElementById('prjsel').innerHTML = selects;
        }
    };

    var row, firstNameCell, lastNameCell;
    var table = document.getElementById("sheet");
    var buttons = table.getElementsByTagName("button");
    for (var i = 0; i < buttons.length; i++) {
        if (buttons[i].name == "edit") {
            buttons[i].onclick = function() {
                row = this.parentNode.parentNode;

                // The first name cell is the first child
                NameCell1 = findElement(row.firstChild);
                NameCell2 = findElement(NameCell1.nextSibling);
                NameCell3 = findElement(NameCell2.nextSibling);
                NameCell4 = findElement(NameCell3.nextSibling);
                NameCell5 = findElement(NameCell4.nextSibling);
                NameCell6 = findElement(NameCell5.nextSibling);
                NameCell7 = findElement(NameCell6.nextSibling);

                // `innerHTML` pour obtenir la valeur
                /*alert("name 1  is " + NameCell1.innerHTML);
                alert("name 2  is " + NameCell2.innerHTML);
                alert("name 3  is " + NameCell3.innerHTML);
                alert("name 4  is " + NameCell4.innerHTML);
                alert("name 5  is " + NameCell5.innerHTML);
                alert("name 6  is " + NameCell6.innerHTML);
                alert("name 7 is " + NameCell7.innerHTML);*/

            }
        }
    }

    xhr.open("POST", "ajaxForm.jsp", true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send("NameCell1="+NameCell1,"NameCell2="+NameCell2,"NameCell3="+NameCell3,"NameCell4="+NameCell4,"NameCell5="+NameCell5,"NameCell6="+NameCell6,"NameCell7="+NameCell7 );
}

从表中获取值后,我想将所有值发送到ajaxForm.jsp


阅读 314

收藏
2020-06-10

共1个答案

小编典典

从最后一行:

xhr.send("NameCell1="+NameCell1,"NameCell2="+NameCell2,"NameCell3="+NameCell3,"NameCell4="+NameCell4,"NameCell5="+NameCell5,"NameCell6="+NameCell6,"NameCell7="+NameCell7 );

这不是在JavaScript中连接字符串的方法。

由于您正在使用JSP,因此您也应该了解Java。您应该以与Java中相同的方式连接JavaScript中的String:

xhr.send("NameCell1=" + NameCell1 + ",NameCell2=" + NameCell2 + "etc...");

也就是说,这在JavaScript控制台中应该已经出错。你注意了吗?无论如何,为了更好地进行JavaScript调试,我建议您抓住Firebug,并尽量减少冗长/不透明和跨浏览器兼容的Ajax处理和HTML
DOM遍历,强烈建议您看一下jQuery。使用jQuery和Ajax表单插件,您只需要准备以下几行:

$(document).ready(function() {
    $('#formId').ajaxForm(function(response) {
        $('#prjsel').html(response);
    });
});

这样,您就不必担心浏览器的特定详细信息以及如何正确发送请求。

2020-06-10