简体   繁体   English

Ajax函数无法打开页面

[英]ajax function not opening a page

i don't understand why ajax is not working. 我不明白为什么Ajax无法正常工作。 my code: 我的代码:

<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
    <script type="text/javascript">
    function edit_row(id)
    {
        $.ajax({
            method:'get',
            url:'form.php',
            success:function(data)
            {
              $('#form_div').html(data);
            }
        });

    }
<?php
   echo '<td style='.$style.'>'.$status.'<a href="" title="Edit"  onClick=edit_row('.$data['type_id'].')><img src="images/pencil.png" width="30px" height="30px"></a></td></tr>';
?>

its not opening form.php onclick what is the problem please help me!!! 它没有打开form.php onclick是什么问题,请帮帮我!!!

You click the link. 您单击链接。 The JavaScript runs. JavaScript运行。 The link is followed. 链接被跟随。 A new page (with the same URL because you have href="" ) loads. 将加载一个新页面(具有相同的URL,因为您拥有href="" )。 The JavaScript stops because its environment has gone away. JavaScript停止是因为其环境已消失。

Use a button instead. 请改用按钮。

you have to put 你必须把

href="javascript:void(0)"

instead of 代替

href=""

else it will reload the page without calling the ajax 否则它将重新加载页面而不调用ajax

You are not passing the 'id' to you form.php I guess you need to pass it: 您没有将'id'传递给form.php,我想您需要传递它:

<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
    <script type="text/javascript">
    function edit_row(id)
    {
        $.ajax({
            method:'get',
            url:'form.php',
            data: {id: id },
            success:function(data)
            {
              $('#form_div').html(data);
            }
        });

    }
</script>

I would do something like this (does not use onClick attribute): 我会做这样的事情(不使用onClick属性):

<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript">
    $(function() {
        $('a.edit').click(function(e) {    // or: $.on('click', 'a.edit', function(e) {
            e.preventDefault();
            var id = $(this).attr('id');
            $.ajax({
                method:'get',
                url: 'form.php',
                data: {id:id},
                success: function(data) {
                    $('#form_div').html(data);
                }
        });
    });
</script>

<?php
    echo '<td style="$style">$status
          <a href="#" id="' . $data['type_id'] . '"  title="Edit" class="edit">
          <img src="images/pencil.png" width="30px" height="30px">
          </a></td></tr>';
?>

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

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