1

次のようなさまざまな行を持つテーブルを作成しました。評価の開始にはrateyを使用します。

質問表

while ($row = pg_fetch_assoc($rs)) {
    echo "<tr>";
    echo "<td>" . $row['question_id'] . "</td>";
    ...
    echo "<td><div> \r \n <div id=\"score-question" . $row['question_id'] ."\" data-score=\"" . $row['rating'] . "\"></div> \r \n </div> \r \n</td>";
    ...
    echo "</tr>";
}

<script type="text/javascript">
    $(function() {
        $.fn.raty.defaults.path = '../plugins/raty/lib/img';

        $('#score-question1').raty({
            readOnly: true,
            noRatedMsg: "Press edit to change the rating",
            score: function() {
                return $(this).attr('data-score');
            }
        });

        $('#score-question2').raty({
            readOnly: true,
            noRatedMsg: "Press edit to change the rating",
            score: function() {
                return $(this).attr('data-score');
            }
        });

        $('#set-action').on('click', function() {
            var options = $('#set-function-demo').val(),
                    command = "$('#function-demo').raty('set', " + options + ");";
            eval(command);
        });
        $('#destroy-action').on('click', function() {
            $('#function-demo').raty('destroy');
        });
    });
</script>

星評価バーを制御するために JavaScript 関数を使用します。同じ名前の 2 つの div を持つことはできないため、別の名前を使用する必要がありますが、各評価バーに対して個別の関数を使用する必要があります。この問題を解決するためのより良い方法はありますか? 私はJavaScriptの経験がないので、おそらく初心者の質問です

4

2 に答える 2

3

詳しく説明すると:

$('.ratyClass').raty({
            readOnly: true,
            noRatedMsg: "Press edit to change the rating",
            score: function() {
                return $(this).attr('data-score');
            }
        });

$(this) を使用すると、関数が呼び出されたオブジェクトにのみ作用し、他の$('.ratyClass')オブジェクトには影響しません。

少し読むと、物事がより明確になるかもしれません: http://www.quirksmode.org/js/this.html

于 2013-09-13T10:46:41.713 に答える