根据用户点击的内容做一些不同的事情 [英] Do something different depending on what the user clicks on

查看:112
本文介绍了根据用户点击的内容做一些不同的事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个链接:

 < a name =send1href =#signup> go 1·; / A> 
< a name =send2href =#signup> go 2< / a>
< a name =send3href =#signup> go 3< / a>

当用户点击链接时,会显示相同的弹出窗口:

 < div id =signup> 
< form action =send_form.php>
< input name =texttype =text>
< button type =submit>发送< / button>
< / form>
< / div>

当用户点击发送时,执行send_form.php。我想检查哪个链接被点击,并做一些不同的事情。




解决方案

你无法仅仅从PHP中知道这一点。这是你可以通过使用javascript(或更好的,jQuery)找到的东西。



方法是:


  • 在所有锚点标记上捕获点击事件
  • 将名称(或更好的ID)分配给变量

  • 将变量的ID添加到形式为隐藏的字段中

    $ b 这是使用jQuery的样子:
  • p>

    jsFiddle演示

    HTML:

     < a name =send1id =send1href =#signup >去1< / a> 
    < a name =send2id =send2href =#signup> go 2< / a>
    < a name =send3id =send3href =#signup> go 3< / a>

    < div id =注册>
    < form action =send_form.php>
    < input name =texttype =text>
    < input name =hidden_​​fieldid =hidden_​​fieldtype =hidden/>
    < button type =submit>发送< / button>
    < / form>
    < / div>

    javascript:

      var the_anchor; 
    $ b $('#注册')。dialog({
    autoOpen:false,
    close:function(){
    $(this).dialog('close ');
    }
    });
    $ b $('a')。click(function(){
    the_anchor = $(this).attr('id');
    $('#hidden_​​field') .val(the_anchor);
    $('#signup')。dialog('open');
    });






    请注意,如果您使用jQuery,则必须参考文档< head> 标记中的jQuery库: C>< HEAD>
    < script src =// ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js\"> ;</script>
    < link rel =stylesheethref =// ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/flick/jquery-ui.css/>
    < script src =// ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js\"> ;</script>
    < / head>


    I have three anchor links:

    <a name="send1" href="#signup" >go 1</a>
    <a name="send2" href="#signup" >go 2</a>
    <a name="send3" href="#signup" >go 3</a>
    

    When the user clicks a link the same pop up form is shown:

        <div id="signup"> 
            <form action="send_form.php">                     
                <input name="text" type="text">
                <button type="submit">Send</button>
            </form>
        </div> 
    

    "send_form.php" is executed when the user clicks "Send". I want to check which link was clicked, and do something different.

    解决方案

    You could not know this simply from PHP. This is something you would find out by using javascript (or better yet, jQuery).

    The approach would be to:

    • trap the click event on all anchor tags
    • assign the name (or better yet, ID) to a variable
    • add the variable's ID to a hidden field in the form

    This is what it would look like using jQuery:

    jsFiddle demo

    HTML:

    <a name="send1" id="send1" href="#signup">go 1</a>
    <a name="send2" id="send2" href="#signup">go 2</a>
    <a name="send3" id="send3" href="#signup">go 3</a>
    
    <div id="signup">
        <form action="send_form.php">
            <input name="text" type="text">
            <input name="hidden_field" id="hidden_field" type="hidden" />
            <button type="submit">Send</button>
        </form>
    </div>
    

    javascript:

    var the_anchor;
    
    $('#signup').dialog({
        autoOpen:false,
        close: function(){
            $(this).dialog('close');
        }
    });
    
    $('a').click(function() {
        the_anchor = $(this).attr('id');
        $('#hidden_field').val(the_anchor);
        $('#signup').dialog('open');
    });
    


    Note that if you are using jQuery, you must reference the jQuery libraries in the <head> tags of the document:

    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/flick/jquery-ui.css" />
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
    </head>
    

    这篇关于根据用户点击的内容做一些不同的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆