繁体   English   中英

Onclick href事件无效

[英]Onclick href event not working

我正在尝试使用onclick事件来获取href的值,以将其添加到输入中,这是我正在使用的方法:

$(document).ready(function()
{
  $(".sellorderprice").click(function(e)
  {
    e.preventDefault();
    $('#buyprice').val($(this).attr('href'));
    $(this).attr('href');
    return false;
  });
  $(".sellorderamount").click(function(e)
  {
    e.preventDefault();
    $('#buyamount').val($(this).attr('href'));
    $(this).attr('href');
    return false;
  });
selldone();
sellupdate();
});

function selldone(){
setTimeout(function(){ sellupdate(); selldone(); },5000);
}
function sellupdate()
{
$.getJSON("<?php echo URL;?>/dashboard/sellorders", function(data){
    $("#sellingorders").find('tbody').empty();
    $.each(data.result, function(){
    $("#sellingorders").find('tbody').append("<tr><td><a href=\"#"+this['price']+"\" class='sellorderprice\'>"+this['price']+"</a></td><td><a href="+this['price']+" class=\"sellorderamount\">"+this['amount']+"</a></td><td>"+this['cost']+"</td></tr>");
    })
});
}

下面的代码将数据追加到可以工作的表格中,然后将价格和金额包装在href中,以便可以启动sellorderprice点击功能和其他功能。 但是,它不起作用,它只是访问链接。

我已经检查了调试控制台,但没有显示任何错误,我的输入是正确的。 我是否以错误的顺序订购功能? 这只是我能想到的。

问题在于绑定事件处理程序的方式。

$(".sellorderprice").click(function(e){

  });
  $(".sellorderamount").click(function(e){

  });

查看您的代码,您将动态创建这些链接并将其附加到某个表中。 但是click()仅在定义这些点击处理程序时元素已经存在于DOM中时才起作用。 因此,您需要像这样在委托模式下使用on() (在JQuery 1.7+中)

     /* Assuming sellingorders is a static element, if this is also 
  some dynamically added element, change main selector to closest static 
  parent (or) document if there's none*/

    $("#sellingorders").on('click','.sellorderprice, .sellorderamount',function(e){
          e.preventDefault();
          if(this.className == 'sellorderprice'){
              $('#buyprice').val($(this).attr('href'));
          }else{
              $('#buyamount').val($(this).attr('href'));
          }
    });

希望这可以帮助 :)

暂无
暂无

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

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