【问题标题】:Removing string from search filter made with Jquery从使用 Jquery 制作的搜索过滤器中删除字符串
【发布时间】:2013-04-02 23:50:01
【问题描述】:

我正在使用这个脚本来过滤表格中的结果。问题是电话号码的格式为 xxx-xxx-xxxx,当您搜索时,如果省略连字符,搜索将错过该条目。有没有办法在搜索中忽略连字符?我试过修改省略空格的行,但我根本无法让它工作。任何帮助将不胜感激。

$(document).ready(function () {
    var $orders = $('#orders tbody .filter');
    $('#filtersearch').keyup(function () {
        var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
        $orders.closest('tr').hide().each(function () {
            if ($('td', this).filter(function () {
                var text = $(this).text().replace(/-\s+/g, ' ').toLowerCase();
                return text.indexOf(val) != -1;
                s
            }).length > 0) $(this).show();
        })
    });
    $('#filtersearch').keyup();
});

还有我的代码

        // connect to the database
        include('connect-db.php');


        // get results from database
        $result = mysql_query("SELECT * FROM orders ORDER BY Name ASC") 
                or die(mysql_error());  

        // loop through results of database query, displaying them in the table
        while($row = mysql_fetch_array( $result )) {

         // echo out the contents of each row into a table
                echo "<tr>";
                echo '<td>' . $row['date'] . '</td>';
                echo '<td class="filter">' . $row['Name'] . '</td>';
                echo '<td class="filter">' . $row['Company'] . '</td>';
                echo '<td class="filter">' . $row['Phone'] . '</td>';
                echo '<td><img src="images/icons/'.$row['Location'].'.png"></td>';
                echo '<td><a href="delete.php?id=' . $row['id'] . '"><img src="images/icons/checkmark.png"></a></td>';
                echo "</tr>"; 
        } 


?>
</tbody>

【问题讨论】:

    标签: jquery search replace


    【解决方案1】:

    999-999-999".replace(/-\s+/g, ' ') 将检查 both 连字符和空格字符。使用它来检查其中一个:

    "999-999-999".replace(/[-\s]+/g, ' ')  // "999 999 999"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 2022-01-16
      • 2023-03-29
      相关资源
      最近更新 更多