0

私はjQueryが初めてで、関数を理解するのに問題があります。

私はHTMLコードの次の構造を持っています:

<ul class="result">
   <li id="a"></li>
   <li></li> //will be added through a loop depending on input
</ul>

そして、このjQueryコード:

$(document).ready(function() { //changes are possible when the page has fully loaded
    $('.inp').keyup(function() { //executes keyup function to the field class "inp"
        var inp = $(this).attr('value'); //sets a variable with the value of input from class="inp"
        $.post('ajax/file.php', {inp:inp}, function(data) { //posts that value to file.php as variable inp and executes the function (data) to this 
            $('.result').html(data);  //adds the returned result into HTML content as a first element in the set of class="result" => <li>

            $('.result li').click(function() { //executes click function when clicking li element
                var result_value = $(this).text(); //sets a variable and prepares something as text 
                $('.inp').attr('value', result_value); //fills value attribute of field class "inp" with the variable result_value
                $('.result').html(''); //???
            });
        });
    });
});

今私の質問は何$('.result').html('');ですか?

4

3 に答える 3

1

JQuery .html() プロパティは、マップ先のオブジェクトの html を設定します。これは、javascript プロパティ .innerHTML と同じ動作をします。

したがって、ここであなたのシナリオでは $('.result').html(''); 結果クラス要素の html を null に設定します。

<ul class="result">
</ul>

次に、「file.php」で間違ったアプローチを使用しています。代わりに次のコードを使用してください。

echo '<li>'.$row["name_1"].'</li>';
echo '<li>'.$row["name_2"].'</li>';
echo '<li>'.$row["name_3"].'</li>';
于 2013-04-17T10:02:22.730 に答える
0

$('.result').html('');の html を.result空白文字列に設定します。

于 2013-04-17T09:46:14.200 に答える