dongmian5325 2015-11-24 20:01
浏览 24
已采纳

提交没有表格的投入

I am trying to edit my table rows (img: http://imgur.com/yTpfCIc ) and POST the changed data to my edit.php file. I am trying to do this via jQuery. But when I click save button nothing happens, this is the explained javascript:

//Getting all "Edit" buttons in the table (one for each row)
var buttons = document.getElementsByClassName("clicker");

var savebutton = function(id){
    //Alert the Id of the button to see if the function is being called, and it is.
    alert(id);
    //If I have any $_POST["action"] different from "update" or "edit" I should get redirected to a page apologizing, saying this cant happen. But nothing happens. (not problem with edit.php, because already tried via another way of posting from another page)
    $.post( "edit.php", { action: "test"} );
};

var buttonclicked = function(e){
    if(e.target.textContent == "Edit")
    {         
        //In this function I create a lot of inputs that you see in the picture and cannot be submited one at a time
        e.target.textContent = "Cancel";
        var id = e.target.id;
        var editable_elements = document.querySelectorAll("[contenteditable=false]");
        var sub = document.getElementById("sub"+id);  

        var j = document.createElement("input");
        j.setAttribute("type", "text");
        j.setAttribute("name", "subject");
        j.setAttribute("value", sub.textContent);
        j.setAttribute("placeholder", sub.textContent);
        j.setAttribute("style", "width: 150px");
        j.textContent = sub.textContent;
        sub.innerHTML = "";
        sub.appendChild(j);

        for(var k = (id*6); k < (id*6)+6; k++){
            var l = k;
            var index = k -(k*id) + 1;

            l = document.createElement("input");
            l.setAttribute('type',"number");
            l.setAttribute("style", "width: 75px");
            l.setAttribute("step", "0.01");
            if(index <= 4){
                l.setAttribute('name',"g"+index);
                l.setAttribute('placeholder',"G"+index);
                l.setAttribute("value", editable_elements[k].textContent);
            }
            else if(index == 5){
                l.setAttribute('name',"creditos");
                l.setAttribute('placeholder',"credits");
                l.setAttribute("value", editable_elements[k].textContent);
            }
            else if(index == 6){
                l.setAttribute('name',"criteria");
                l.setAttribute('placeholder',"criteria");
                l.setAttribute("value", editable_elements[k].textContent);
            }
            editable_elements[k].innerHTML = "";
            editable_elements[k].appendChild(l);}

        //If any edit button is pressed, create a save button in the same row 
        var s = document.createElement("input");
        s.textContent = "Save";
        s.setAttribute('type',"button");
        s.setAttribute('value',"update");
        s.setAttribute("id", id);
        s.setAttribute("name", "a");

        //Call the function that is supposed to POST all inputs infotmations
        s.setAttribute("onclick", "savebutton(this.id)");
        var clickbutton = document.getElementById("save"+id);
        clickbutton.appendChild(s);

    }
    else //save button has been clicked
    {
        //nothing
    }
};

//If one of those buttons is clicked call the function
for(var j = 0; j < buttons.length; j++)
{
    buttons[j].addEventListener('click', buttonclicked);
}

That is my problem... If you want to see the whole page, its here: http://pastie.org/10578782

  • 写回答

1条回答 默认 最新

  • dongzhu7329 2015-11-24 20:06
    关注

    You have this comment in your code:

    //If I have any $_POST["action"] different from "update" or "edit" I should get redirected to a page apologizing, saying this cant happen. But nothing happens. (not problem with edit.php, because already tried via another way of posting from another page)

        $.post( "edit.php", { action: "test"} );
    

    When you post via ajax, redirects do not work. Open the dev tools in your browser and check the network traffic. I'm pretty sure the edit.php page is being posted to. You need to use a callback function to check the response of the post action and act accordingly, example:

    $.post( "edit.php", { action: "test"}, function(data) {
      //data is the response from the edit.php
      alert(data);
    });
    

    Try this code and see what the alert box says. If you want to "redirect", you can use document.location.href = 'whatever.php'; inside the callback in place of the alert(); statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置