简体   繁体   English

javascript更新功能不起作用

[英]javascript update function doesn't work

I have the following javascript code which is supposed to open up a modal window with three fields to be modified and then send them over to be stored in the database, the thing is it doesn't work as it's supposed to, for some unknown reason to me when i modify any of the fields such as area or nivel and hit the "send" button it doesn't sends over the new data but the previous one its like the data gets fixed when the page is loaded and not when i hit the send button, here is the code: 我有以下javascript代码,应该打开一个带有三个要修改字段的模态窗口,然后将它们发送过来存储在数据库中,但由于某种未知的原因,它无法正常工作对我来说,当我修改诸如area或nivel之类的任何字段并单击“发送”按钮时,它不会发送新数据,但是上一个类似的数据在页面加载时是固定的,而不是在我点击时发送按钮,这是代码:

function mostrar(enlace, id, nivel, area, trabajo) {
  document.getElementById("btnEditar").onclick = function() {
    var area = document.getElementById("area").value;
    var nivel = document.getElementById("nivel").value;
    var trabajo = document.getElementById("area_de_trab").value;
    editarRegistro(id, area, nivel, trabajo);
  }
}

i also have to say that function "mostrar" is an onclick event handler, any suggestions on how can i fix this? 我还必须说函数“ mostrar”是一个onclick事件处理程序,有关如何解决此问题的任何建议? Thanks in advance. 提前致谢。

Here is the button to show up the modal window which at the same time attach the event handler to the send button which is placed into the modal window: 这是显示模式窗口的按钮,该窗口同时将事件处理程序附加到放置在模式窗口中的发送按钮:

while($row = mysql_fetch_array($resultado)) 
    {

        echo "<tr>";
        echo "<th>".$row["idusuario"]."</th>";
        echo "<th>".$row["nombre"]."</th>";
        echo "<th>".$row["username"]."</th>";
        echo "<th>".$row["area"]."</th>";
        echo "<th>".$row["area_de_trab"]."</th>";
        echo "<th>".$row["nivel"]."</th>";                              
        echo "<th><a type='submit' value='Eliminar' name='id' onclick='eliminar($row[idusuario])' class='btn btn-success'><span class='glyphicon glyphicon-trash'></span></a>
                            <a type='submit' href = '#miventana' value='editar' onclick='mostrar(this,$row[idusuario],\"".$row['nivel']."\",\"".$row['area']."\",\"".$row['area_de_trab']."\")' data-toggle='modal' class='btn btn-success'><span class='glyphicon glyphicon-pencil'>
                            </span></a></th>";
                            echo "</tr>";
                    }

and the button to send the data: 和发送数据的按钮:

<button type="submit" class="btn btn-primary" name="editar" value="editar" id="btnEditar">guardar</button>

you need to separate the named function from the onclick handler: 您需要将命名函数与onclick处理程序分开:

 document.getElementById("btnEditar").onclick = mostrar;


function mostrar()
 {var area = document.getElementById("area").value;...

and then because you are getting the values in that function you don't need to pass them as arguments in the function call. 然后,因为您正在该函数中获取值,所以不需要在函数调用中将它们作为参数传递。

You just pass 'id' to onclick function 您只需将“ id”传递给onclick函数

You can rewrite this code like this 您可以像这样重写此代码

function mostrar(enlace, id, nivel, area, trabajo) {
  document.getElementById("btnEditar").onclick = function(idt) {
    var area = document.getElementById("area").value;
    var nivel = document.getElementById("nivel").value;
    var trabajo = document.getElementById("area_de_trab").value;
    editarRegistro(idt, area, nivel, trabajo);
  }(id);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM