在数据库中循环值时,为输入框获取唯一值


Get unique value for input box when looping through values from a database

我正在使用javascript作为搜索函数。

function searchbrand(){
    var searchTxt = $("input[name='brandid']").val();
    $.post("search-brand-vw.php", {searchVal: searchTxt})
    .done(function(brandlist) {
        $("#brandlist").html(brandlist);
    });
}

在这里,我将一个输入框的值发送到一个单独的php文件。因此,在这个页面中,我将从mysql数据库中填充一个表。因此存在许多brandid字段。对于表的第一行,搜索工作正常。但是,当第一行有一个值时,当我在第二行进行搜索时,它也会从第一行获得值。如何才能仅在我正在键入的行上获取值。仅供参考,每行中都有一个名为DL_Id的字段,它是唯一的。我如何将该值用于此行,也许可以与AND子句一起使用?

var searchTxt = $("input[name='brandid']").val();

这就是我从数据库循环的方式

$query=mysqli_query($link,"SELECT * FROM dl_commfactory_2 LIMIT 35");
while($row = mysqli_fetch_array($query)){
    $dlid = $row['DL_Id'];
    $brandid = $row['Brand_Id'];
    ?>
    <tr>
    <td><input type="text" name="dlid" readonly value="<?php echo $dlid; ?>" size="2"></td>     
    <td><input type="text" name="brandid" value="<?php echo $brandid; ?>" list="brandlist" id="output" onkeyup="searchbrand()" >
    <datalist id="brandlist" name="taskoption">
        <option> </option>
    </datalist> </td>
    <?php } ?>

首先,将searchBrand()的调用更改为searchBrand。

然后,相应地更改searchBrand功能:

function searchBrand(el){
    var brandIdElement = $(el); //The element being edited
    var searchTxt = brandIdElement.val(); //The new value sent
    var searchTxtDL = brandIdElement.parents('tr').find('input[name="dlid"]').val(); //The dlid element's value in the same line
    //Send the value of the dlid too
    $.post("search-brand-vw.php", {searchVal: searchTxt, searchValDL: searchTxtDL})
    .done(function(brandlist) {
        //Get the brandList element for the specific line
        var brandListEl = brandIdElement.parents('tr').find('datalist[name="taskoption"]');
        brandListEl.html(brandlist);
    });
}

可以实现一些性能优化,但如果这里的性能不是问题,那么它应该是可以的。