简体   繁体   English

如何修剪 <ul> 和 <li> 标签,但值 <li> 会保持不变吗?

[英]How to trim <ul> and <li> tag, but the value of <li> will be the remain same?

I have created an ajax method to search for User Lists. 我创建了一个ajax方法来搜索用户列表。 But the ajax function returning me such as : 但是ajax函数返回我,例如:

 <ul><li>User Name</li></ul>

I want to trim <ul></ul> and <li></li> tag in main page to use the exact value. 我想修剪主页中的<ul></ul><li></li>标签以使用确切的值。
Like : 喜欢 :

Before : 之前:

<ul><li>User Name</li></ul>

After : 之后:

User Name 用户名

The ajax result will store in : Ajax结果将存储在:

<div class="input-group" id="userList"></div>

I have done already some javascript method to do so 我已经做过一些javascript方法

Ajax function : Ajax功能:

< script >
    $(document).ready(function() {
    $('#run').keyup(function() {
        var query = $(this).val();
        if (query != '') {
            $.ajax({
                url: "userSearch.php",
                type: "POST",
                data: {
                    query: query
                },
                success: function(data) {
                    $('#userList').fadeIn();
                    $('#userList').html(data);
                    paymentStatus(data);
                }
            });
        } else {
            $('#userList').fadeOut();
        }
    });
    $(document).on('click', 'li', function() {
        $('#run').val($(this).text());
        $('#userList').fadeOut();
    });
    }); <
/script>

userSearch.php File Code : userSearch.php文件代码:

<?php
require_once '../../config/config.php';
if(isset($_POST['query']))
{
    $output = '';
    $query = "SELECT Name, Id FROM ucoe_users WHERE Name LIKE '%".$_POST['query']."%' AND RoleId = 3";
    $result = mysqli_query($connection, $query);
    $output = '<ul>';
    if(mysqli_num_rows($result)> 0 )
    {
        while($row = mysqli_fetch_array($result))
        {
            $output .= "<li>".$row['Name']."</li>";
        }
    }
    else
    {
        $output .= '<li>User Not Found</li>';
    }
    $output .= "</ul>";
    echo $output;
}
?>

Use $(html).text() to get text value from your html 使用$(html).text()从HTML获取文本值

           success: function(data) {
                $('#userList').fadeIn();
                $('#userList').html($(data).text());
                paymentStatus(data);
            }

 var html = '<ul><li>User Name</li></ul>'; console.log($(html).text()); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

You can use the jQuery as following , 您可以按以下方式使用jQuery,

    var arrOfValues = [];
    $('ul li').each(function(i)
    {
       arrOfValues.push($(this).text()); //every value of ul li will be push in arrOfvalues.

    });
$('#userList').fadeOut();

If it was a DOM element then it would be straight forward just get innerText 如果它是一个DOM元素,那么直接获取innerText就可以了。

for eg : 例如:

<ol>
<li>User Name</li>
</ol>

<script>
var olDom = document.getElementsByTagName("ol");
console.log(olDom[0].innerText)
</script>

But in your case you are getting HTML as a response of Ajax, so first we need to create a wrapper div to hold your response in a form of DOM and then we will call innerText. 但是在您的情况下,您将HTML作为Ajax的响应,因此首先我们需要创建一个包装器div,以DOM形式保存您的响应,然后再调用innerText。

<script>
var wrapperDiv = document.createElement('div');
wrapperDiv.innerHTML = data;
$('#userList').html(wrapper.innerText);
</script>

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

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