简体   繁体   English

简单的ajax onclick问题

[英]Simple ajax onclick question

I'm having difficulty setting up a simple menu that displays results on a div box: 我在设置一个在div框上显示结果的简单菜单时遇到了困难:

I have a list of 10 links which link to php files on my server that return data. 我有一个10个链接的列表,链接到我的服务器上返回数据的PHP文件。 I would like it so that when the viewer clicks on one of the links, the data from the php file will display on a div box on the screen, and then when he clicks on another link, it will display the data from that php file in the div box (replacing the previous text). 我希望这样当观众点击其中一个链接时,来自php文件的数据将显示在屏幕上的div框中,然后当他点击另一个链接时,它将显示来自该php文件的数据在div框中(替换以前的文本)。

I know this is pretty simple, but I can't get it to work. 我知道这很简单,但我不能让它工作。 I'm using jQuery, and would like to know if any of you have any quick solutions. 我正在使用jQuery,并想知道你们是否有任何快速的解决方案。

Thanks!! 谢谢!!

UPDATE: I've been pretty much failing javascript code-wise. 更新:我几乎失败了javascript代码。 But here is the basic idea for the framework: 但这是框架的基本思想:

 <div class="tabs">
        <ul class="tabNavigation" style="float:left; padding:1px;">
            <li><a href="#displayphpfile">Load phpfile1</a></li>
            <li><a href="#displayphpfile">Load phpfile2</a></li>
            <li><a href="#displayphpfile">Load phpfile3</a></li>
        </ul>
        <div id="displayphpfile">
            <p>Load phpfile1</p>
        </div>
    </div>

jQuery has a specific method for doing that: load(). jQuery有一个特定的方法:load()。

I would change your example a little though, so the hrefs of the links point to the php file: 我会稍微改变你的例子,所以链接的hrefs指向php文件:

<div class="tabs">
    <ul class="tabNavigation" style="float:left; padding:1px;">
        <li><a href="phpfile1.php">Load phpfile1</a></li>
        <li><a href="phpfile2.php">Load phpfile2</a></li>
        <li><a href="phpfile3.php">Load phpfile3</a></li>
    </ul>
    <div id="displayphpfile">
        <p>Load phpfile1</p>
    </div>
</div>

Then the code is very simple: 然后代码很简单:

$(document).ready(function() {
   $(".tabNavigation a").click(function() {
      $("#displayphpfile").load($(this).attr("href"));

      return false;
   });
});

I haven't tested this, but is this close to what you want? 我没有测试过这个,但这是否接近你想要的?

<script type="text/javascript">
    $(document).ready(function() {
        $('a').click(function() {
            var file = $(this).text().toLowerCase() + '.php';
            $.ajax({
                url:file,
                cache:false,
                success: function(response) {
                    $('#data_goes_here').html(response);
                }
            });
            return false;
        });
    });
</script>
<ol>
    <li><a href="#">Foo</a></li>
    <li><a href="#">Bar</a>
    <li><a href="#">Baz</a></li>
</ol>

<div id="data_goes_here"></div>

Bipedal Shark is calling a document.ready inside a click element! Bipedal Shark在点击元素中调用document.ready!

It should be: 它应该是:

<script type="text/javascript">
    $(document).ready(function() {
        $('a').click(function() {
            var file = $(this).text().toLowerCase() + '.php';
            $.ajax({
                    url:file,
                    cache:false,
                    success: function(response) {
                            $('#data_goes_here').html(response);
                    }
                });
            return false;
            });
        });
</script>

But the load method is much cleaner code. 但是加载方法代码更清晰。

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

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