douyin9987 2013-02-25 22:30
浏览 21
已采纳

JS:更改此功能以处理链接点击

Following the example here Very Simple jQuery and PHP Ajax Request – Ready to use code

I've been successful in creating a drop down list that passes the value to an external PHP script and returns the HTML output back to a "div" on the same page and it works great.

What I want to do now is post values when I click on link instead of building a drop down list. So ...if I created this link:

<a href="foo.php?route_number=2">Route Number 2</a>

I want "2" passed to that external PHP script and the content changed on the " div " as it currently works with the dropdown. I don't know how to change the javascript to handle this or what "foo.php" really needs to be.

Here's the current javascript from that example:

    <script type="text/javascript">
    $(document).ready(function() {
        $('#route_number').click(function() {
            routenumber = $('#route_number').val();
            $.post('api.php', { route_number : routenumber }, function(res) {
                $("#mainlayer").html(res);
            });
         });
    });
    </script>

And here's what the dropdown portion of the HTML looks like:

    <select name="route_number" id="route_number">
    <option value="notchosen">Please Choose A Route</option>
    <option value="2">Riverfront</option>
    <option value="11">Magazine</option>
    <option value="16">Claiborne</option>
    </select>
    <div id="mainlayer">

    </div>

So, to be clear, instead of a dropdown that passes values, I want to create links that accomplish the same result.

Thanks in advance,

  • dan -
  • 写回答

4条回答 默认 最新

  • donoworuq450547191 2013-02-25 23:14
    关注

    I had hoped simply fixing @LifeInTheGrey's example would've sufficed, but there are some things I would've done differently that probably need some explaining.

    Your HTML could look something like this:

    <a class="route" href="foo.php?route_number=2" data-route="2">Route Number 2</a>
    

    The JavaScript would look something like this:

    $(function() {
        var fill_div_with_response = function(res) {
            $("#mainlayer").html(res);
        };
    
        var handle_error = function(res) {
            alert('something went wrong!');
        };
    
        $(document.body).on('click', '.route', function(event) {
            // prevent the browser's default action for clicking on a link
            event.preventDefault();
            // grab route number from data attribute
            var route = $(this).data('route');
            // make that post request
            $.post('api.php', {route_number: route})
                // handle the response
                .done(fill_div_with_response)
                // handle errors
                .fail(handle_error);
        });
    });
    
    1. The example uses delegated events. They're cheap to initialize and consume the least amount of memory.
    2. The example handles errors. Most answers to questions like these neglect that. errors happen. Always. Make people aware of that. Surely throwing an alert() is not the thing you want to be doing, but it's still better than simply ignoring errors completely.
    3. The example uses Deferreds (Promises) rather than callbacks, as this usually makes code much cleaner.
    4. We're defining the callbacks fill_div_with_response() and handle_error() at the root closure to prevent redefining them on the next click. There's no need to feed the garbage collector.
    5. The data attribute poses the optimal alternative to <option value="123"> in the way that it prevents you from having to parse the href attribute to extract that number from a string.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题