简体   繁体   English

如何在不刷新页面的情况下创建更新按钮

[英]How to create update button without page refresh

I have my items: 我有我的物品:

<table>
    <tr>
        <th>
            id
        </th>
        <th>
            name
        </th>
        <th>
            update
        </th>
    </tr>
    <tr>
        <td>
            <?php echo $id; ?>
        </td>
        <td>
            <?php echo $name; ?>
        </td>
        <td>
            <input type="button" name="id" id="update" value="update" />
        </td>
    </tr>
</table>

Now I want when I click on update button to load update.php?id=$id result under the button How can I make this in JavaScript? 现在,当我单击“更新”按钮以加载按钮下方的update.php?id=$id结果时,我该如何在JavaScript中创建它? Please help me. 请帮我。

Here's a basic example of content load via JavaScript, using the JQuery library: 这是使用JQuery库通过JavaScript加载内容的基本示例:

<table>
    <tr>
        <th>
            id
        </th>
        <th>
            name
        </th>
        <th>
            update
        </th>
    </tr>
    <tr>
        <td>
            <?php echo $id; ?>
        </td>
        <td>
            <?php echo $name; ?>
        </td>
        <td>
            <input class="toLoad" link="update.php?id=<?php echo $id; ?>" type="button" name="id" id="update" value="update" />
        </td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td id="results"></td>
    </tr>
</table>


<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(".toLoad").click( function(event)
{
        event.preventDefault();
        $("#results").load($(this).attr("link"));
});
</script>

Not tested, should work. 未经测试,应该可以工作。 In case it's not working, please reply with the error you are getting. 如果它不起作用,请回复您得到的错误。

First give an id to the where your button is in. 首先给按钮所在的位置提供一个ID。

<td id="main">
    <input type="button" name="id" id="update" value="update" />
</td>

Then include the JQuery library to your <head></head> section 然后将JQuery库包含到您的<head></head>部分

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" ></script>

Add an ajax call when you push the button 按下按钮时添加ajax调用

$(document).ready(function() {
    $('input[name="id"].click(function(){
        $.ajax({
          url : 'update.php',
          type : "POST",
          data : {
                id  : '<?php echo($id); ?>'
          },
          success : function(data) {
            $('#main').append(data);
          }
      });
    });
}

When you click the button an ajax call is made to the php page where you must echo the result, this will be displayed after the button. 当您单击按钮时,将在php页面上进行ajax调用,您必须在其中echo显结果,该页面将显示在按钮之后。

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

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