简体   繁体   English

使用jQuery从UL Li获取ID

[英]Getting an id from a ul li using jquery

I'm populating an unordered list using a keyup jquery function that works fine. 我正在使用工作正常的keyup jquery函数填充无序列表。 However once the information is returned I would like to get the id by clicking on the li: 但是,一旦返回信息,我想通过单击li来获取ID:

This is my function: 这是我的功能:

if( $_REQUEST["assist"] ) {

    $search_sql = "SELECT * FROM `inventory` WHERE upper(name) LIKE upper('%".$_REQUEST['assist']."%') and active = '1' OR upper(part_num) LIKE upper('%".$_REQUEST['assist']."%') and active = '1' ORDER BY `part_num` ASC ";
    $result = mysql_query($search_sql);

    if (mysql_num_rows($result) > 0) {
        // output data of each row
        while ($row = mysql_fetch_array($result)) {
            echo '<li class="assist"><div id="assist"><input type="hidden" class="assist_id" value="'.$row['inv_id'].'">'.$row['part_num'].' | '.$row['name'].'</div></li>';
        }
    } else {
        echo '<h2>There are no parts matching "'.$_REQUEST['assist'].'"</h2>';
    }
}

It is putting the result into this html: 它将结果放入此html中:

 <ul id="assist-search-value"></ul>

My javascript is 我的JavaScript是

 $('#assist-search-value li').click(function() {
    var option = $(this).find('.assist_id').val();
    alert(option);
});

However when i click on the results I'm getting just the first id in the list instead of the one that I would like. 但是,当我单击结果时,我只是列表中的第一个ID,而不是我想要的ID。

Please don't use this in a production environment. 请不要在生产环境中使用它。

1) You're using the mysql PHP library, which is deprecated. 1)您正在使用不推荐使用的MySQL PHP库。 Use mysqli or PDO instead. 改用mysqliPDO
2) You're leaving yourself open to SQL Injection . 2)您可以自由使用SQL Injection At very least, use mysql_real_escape_string . 至少,请使用mysql_real_escape_string
3) HTML ID's must also be unique. 3)HTML ID也必须是唯一的。
4) You also need to close the <input> tag that's opened in your PHP. 4)您还需要关闭在PHP中打开的<input>标记。 <input type="hidden" class="assist_id" value="'.$row['inv_id'].'"/>

However, to answer your question - you can hook up a handler for your li's with class assist . 但是,要回答您的问题-您可以在班级assist为您的li's挂接处理程序。 As they're dynamically added, you need to add the handler to an element which already exists when you bind to it.You can do this using on . 由于它们是动态添加的,因此需要将处理程序添加到绑定到该元素时已经存在的元素。您可以使用on进行此操作。

$('#assist-search-value').on('click', 'li.assist', function() {
    var productId = $(this).find("input.assist_id").val();
    alert(productId);
});

You can refer to this one: 您可以参考以下内容:

use children() instead of find() 使用children()而不是find()

<ul id="assist-search-value">
    <li class="assist"><input type="hidden" class="assist_id" value="1" />test 1</li>
    <li class="assist"><input type="hidden" class="assist_id" value="2" />test 2</li>
    <li class="assist"><input type="hidden" class="assist_id" value="3" />test 3</li>
    <li class="assist"><input type="hidden" class="assist_id" value="4" />test 4</li>
</ul>

and then your jquery may look like this: 然后您的jquery可能如下所示:

$(document).ready(function() {

    $("ul#assist-search-value").on("click", "li.assist", function() {
        alert($(this).children("input.assist_id").val());
    });

});

working demo: http://jsfiddle.net/bqv76xe8/ 工作演示: http : //jsfiddle.net/bqv76xe8/

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

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