简体   繁体   English

jQuery .load结果进入'调用'div(而不是指定的id)?

[英]jQuery .load result into the 'calling' div (rather than a specified id)?

I'm putting together a rather simple 'vote' function - the user clicks on a link, the 'action' page adds the vote to the DB & returns the number of votes. 我正在整理一个相当简单的“投票”功能 - 用户点击链接,“操作”页面将投票添加到数据库并返回投票数。 This is what I currently have; 这就是我现在拥有的;

<a onClick="vote(#tipID#);return-false;" class="post-add-icon inline-items" id="tipVote"><i class="far fa-heart"></i> #tipVotes#</a>

and the JS; 和JS;

<script>
function vote(num){$("#tipVote").load('/assets/a/vote/?vtype=tip&tipID='+num+'');}
</script>

This all works fine, with one exception - I have several divs named 'tipVote' on the page (output is dynamic). 这一切都很好,有一个例外 - 我在页面上有几个名为'tipVote'的div(输出是动态的)。 Whilst I can alter the div names by adding an id (ie tipVote999, tipVote232 etc), I have no way of knowing what those ID's will be prior to the output, and of course it would be incredibly 'messy' to duplicate the code for each id. 虽然我可以通过添加id来改变div名称(即tipVote999,tipVote232等),但我无法知道输出之前这些ID是什么,当然复制代码会非常“混乱”。每个id。

So, my question is whether either of these is possible; 所以,我的问题是这些是否可能;

  • Can I somehow restrict the post back to the div that calls it? 我可以以某种方式将帖子限制回调用它的div吗? I did try (without success); 我确实尝试过(没有成功);

    \nfunction vote(num) { function vote(num){\n   $('this').load('/assets/a/vote/?vtype=tip&tipID=' + num + ''); $('this')。load('/ assets / a / vote /?vtype = tip&tipID ='+ num +'');\n} }\n
  • Or, can I somehow send a variable with the onclick event that also shows the div for post back. 或者,我可以以某种方式使用onclick事件发送变量,该事件也显示回发的div。 In that vein, I also tried (again without success); 在那种情况下,我也尝试过(再次没有成功);

function vote(num){
       $("#tipVote' + num+'").load('/assets/a/vote/?vtype=tip&tipID='+num+'');
    }

I've done a fair amount of searching, but found nothing - but I feel this must be something that's been covered before. 我已经做了相当多的搜索,但一无所获 - 但我觉得这必须是之前已经涵盖过的东西。

Help & Advice much appreciated in advance! 帮助和建议提前非常感谢!

It looks like you were almost there; 看起来你几乎就在那里; the only problem with this was some mis-nested quotes: 唯一的问题是一些错误嵌套的引号:

function vote(num){
   $("#tipVote" + num).load('/assets/a/vote/?vtype=tip&tipID='+num);
}

Assuming the div id is something like "tipVote123", calling either vote(123) or vote('123') will load the data into that div. 假设div id类似于“tipVote123”,则调用vote(123)vote('123')将数据加载到该div中。

Alternatively, you could read the ID from the DOM instead of passing it as a parameter to the vote function, using whatever DOM traversal gets you from the link the user clicked to the specific div you want filled -- but since it looks like you already have the tipID being passed into the function, may as well use that instead of reading the DOM. 或者,您可以从DOM读取ID而不是将其作为参数传递给投票函数,使用任何DOM遍历从用户单击到您想要填充的特定div的链接中获取 - 但是因为它看起来像你已经将tipID传递给函数,也可以使用它而不是读取DOM。

I would do something like this: (assumes '#tipID#' is a dynamically generated unique id) 我会做这样的事情:(假设'#tipID#'是动态生成的唯一ID)

<div id="div_"+#tipID#>
<a href='#' class="tipVote post-add-icon inline-items" id= #tipID# ><i class="far fa-heart"></i> #tipVotes#</a>
</div>

assumes jQuery 假设jQuery

<script>
  $(document).ready(function(){
    $('.tipVote').click(function () {
      var id = $(this).id();

      var jqxhr = $.post( "/assets/a/vote/?vtype=tip&tipID='+id+''", function() {
        //alert( "success" );
        $('#div_'+id).html('<p>Voted</p>');
      })
      .fail(function() {
        alert( "error" );
      });
    });
  });
</script>

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

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