【问题标题】:Get the id of the ajaxLink button before submission提交前获取ajaxLink按钮的id
【发布时间】:2012-09-04 12:18:13
【问题描述】:

我在 Yii 框架(不是 ajax 表单提交按钮)中创建了一个 ajax 链接(CHTML::ajaxLink),它通过 ajax 将一些值传递给控制器​​。有多个链接将不同的值传递给控制器​​。我想在将值传递给控制器​​之前获取单击链接的 id/class 属性(在 jquery.ajax 选项的“beforeSend”中)。只是我只想获取生成 ajax 请求的 id/class 属性。救命!!!

更新::这是代码

echo CHtml::ajaxLink ("Click Here",
                              Yii::app()->createUrl('default/del/id/6'), 
                              array(
                                    'beforeSend' => 'function(){
                        //I want to get the id of the link here     
    }',
                                    'complete' => 'function(){
                            }',

                                    'update' => '#loadContent'),

        );
The above code will generate the following a tag:-
<a href="#" id="yt1">Click Here</a>

当用户单击上面的链接时,我想在 beforeSend 部分中获取 id (yt1) ajax链接。

我尝试了以下代码:

 'beforeSend' => 'function(){
 $("a").click(function(){
    var a = $(this).attr("id");
    alert(a); 
 }

上面的代码有效,但只有在点击链接两次时才会提醒 id。在第三次点击时,id 会收到两次警报,并在随后的点击中不断增加。我对这个奇怪的问题没有任何线索。

【问题讨论】:

  • 在点击处理程序中,您可以通过$(this).attr('id')$(this).attr('class') 获取id 或类。
  • 如果你不想用 jquery 摸索,你可以生成你的链接,以便 id 和 class 是链接中的参数(即 GET 请求)
  • 您会收到多个警报,因为您每次在 beforeSend 中都在链接上注册一个新的点击处理程序。实际上,您应该在 ajax 数组中定义一个“数据”键,在其中设置一个 JSON 键:值,例如 {key1: $(this).attr('id')}

标签: php ajax jquery yii


【解决方案1】:

您可以使用$.proxy(),将函数的上下文更改为当前ajax对象的锚标记:

'beforeSend' => '$.proxy(function(jqXHR,settings){
        console.log($(this).attr("id"));// or alert($(this).attr("id"))
        // rest of your function
        // you can still use the jqXHR object, and the settings map to manipulate the ajax call
},this)'

编辑:

让我告诉你为什么警报数量会随着后续点击而增加。

发生这种情况是因为每次单击时,都会有一个新的单击处理程序与&lt;a&gt; 相关联,因为这行:

$("a").click(function(){...})

所以第一次点击的时候,函数调用的顺序是:

beforeSend callback
assign click handler (1)

所以还没有警报。

第二次:

1st click handler's alert
beforeSend callback
assign click handler (2)

第三次:

1st click handler's alert
2nd click handler's alert
beforeSend callback
assign click handler (3)

等等,随着它不断增加。

编辑2

另一种更好的选择,您可以使用context 选项来制作上下文,即刚刚单击的链接:

'context'=>'js:this', // right now 'this' is link, so we can pass it
'beforeSend'=>'function(){// i would suggest using the signature 'function(jqXHR,settings)' so that 
    // you can modify the ajax call if you need to

    console.log($(this).attr("id"));// or alert($(this).attr("id"))
    // rest of your function
}'

来自jquery's ajax documentation

默认情况下,上下文是一个对象,表示调用中使用的 ajax 设置($.ajaxSettings 与传递给 $.ajax 的设置合并)。

编辑3

另一种选择:将链接的 id 作为附加设置键传递:

'idOfLink'=>'js:$(this).attr("id")',
'beforeSend'=>'function(){
     // now you can access the id like:
     console.log(this.idOfLink);// or alert(this.idOfLink)
}'

【讨论】:

  • 我知道你已经接受了一个答案,但我认为它不是正确的(恕我直言)。因此,请告诉我这对您有何影响,如果您在实施/理解此解决方案或其他反馈方面遇到困难,请要求澄清。
  • 请检查我的新编辑,我解释了为什么您的代码不起作用,如果您需要澄清,请告诉我
  • 顺便说一句,我的代码还会在第一次点击时为您提供链接的 ID!还请告诉我为什么你认为这个答案对你没有帮助。我已经抽出时间并试图回答,所以如果您不介意,请留下一些反馈
  • 非常感谢您对警报的澄清。现在我对我的问题有了清晰的认识。我没有使用您提供的代码,因为我无法弄清楚代理和 jqXHR 的事情。但是你的解释对我很有帮助,所以我接受你的回答。
  • 现在我明白了,我使用了您的代码并且完美运行!!!而且它比使用本地存储要干净得多。非常感谢.......你在 jquery 方面很好......
【解决方案2】:

如果你使用jQuery,你可以这样做来获取元素属性:$("#element_id").attr("id"),或者如果你使用HTML5,你可以在你的链接上使用data标签,比如:

<a href="bla.php" data-name="your_data_here">Link</a>

还可以使用jQuery 你这样做:$("#element_id").data("name")

【讨论】:

  • 您的代码有效,但我无法在第一次单击 ajax 链接时获取 id。无论如何感谢您的回答。感谢您的帮助。
【解决方案3】:

您可能需要详细说明/发布代码片段,以便我们更好地了解您的问题。但是根据您在此处的解释,我假设您希望处理您的 Ajax 请求的任何内容都知道它源自哪个链接。假设异步处理是在其他地方(在您的浏览器之外)完成的,它将无法访问您的 DOM。因此,您需要将 id 作为参数嵌入到您的 ajax 请求中。换句话说,将发送请求的元素的 id 作为请求的一部分传递。

【讨论】:

  • 我需要在将ajax请求发送到服务器之前获取链接的id。我已经添加了代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-27
  • 2019-03-10
相关资源
最近更新 更多