简体   繁体   English

jQuery-Ajax单击href触发器

[英]Jquery - Ajax click on a href trigger

I have a small question. 我有一个小问题。 I have an input field where somebody can search a specific value. 我有一个输入字段,有人可以在其中搜索特定值。 Here is the following code 这是下面的代码

$('#usersearch').keyup(function () { 
    var inputvalue = $('#usersearch').val();
    $('#usersearch').val(inputvalue);
    document.getElementById("popupwithuserssearch").style.display = "block"; 
    $.ajax({
        type: "GET",
        url: "usersearch.php",
        data: {
            input: inputvalue
        },
        success: function(msg) {
            $('#popupwithuserssearch').html(msg); 
        }
    });
})

After i run my query in usersearch.php I echo the html to generate a table : 在usersearch.php中运行查询后,我回显html以生成表:

$cell='1';
while (!$rs->EOF){
    echo '<tr>';
    echo '<td align="left" class="cell-'.$cell.'" bgcolor="#F0F0F0"><a href="#" style="color:black;" id="clicked">'.$rs->fields('name').'</a></td><br>';
    $cell = $cell+1;
    $rs->movenext();
    echo ' </tr>';
} ';

now i'm trying to get the value if the a href is clicked i tried this already: 现在我正在尝试获取值,如果单击了href,我已经尝试过了:

$("#clicked").click(function () {
    var value = $(".testClick").attr("href");
    alert(value );
});

I'm probably looking over it can somebody help me out 我可能正在寻找可以有人帮我的忙

Thanks! 谢谢!

Well, remove your ID cause it must be a unique, and assign a class instead. 好吧,删除您的ID,因为它必须是唯一的,然后分配一个类。
Try this code 试试这个代码

$(".clicked").click(function () {
   var value = $(this).attr("href");
   alert(value);
});

To prevent a redirect by clicking on your A tag add following line event.preventDefault(); 为了防止通过单击A标记进行重定向,请添加以下代码行event.preventDefault(); to your click function. 单击功能。

Since no value to href at the moment I'm displaying the ID 由于目前没有显示href值,因此我正在显示ID

 // Add content dynamically $(document).ready(function(){ var content = '<table>'; for(var i = 0 ; i < 10 ; i++){ content += '<tr><td align="left" class="cell-'+i+'" bgcolor="#F0F0F0"><a href="#" style="color:black;" class="clicked" id="clicked_'+i+'">'+i+'</a></td></tr>' } content += '</table>'; $('#add-dynamic-content').append(content) }); // Click event $(document).on('click','.clicked', function(){ alert($(this).attr('id')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='add-dynamic-content'></div> 

$(document).on('click', '#clicked', function () {
    var value = $(".testClick").attr("href");
    alert(value );
});

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

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