2017-08-09 12 views
0

出典:GitHub Dobtco starrr JS pluginJSスターの評価でMYSQLの<span>の価値を取得する方法は?

は異なる分野でのMySQLデータベース上の&ストアデータを、会社を検討するユーザーのために、このプラグインを使用します。

更新日: javascriptファイルを編集して、2つの異なるレビューフィールドにスターを使用して複数回使用しました。

符号化部:あなたがinput使用val()を更新/読み出しするとK Scandrett

<form method="post" name="review" action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>"> 
    <div id="collapse1" class="panel-collapse collapse"> 
<div class="panel-body"> 
     <div class="form-group"> 
    Review 1: <div id="stars_1" class="starrr" name ="rv_1" data-rating=<?php echo $rv_1 ?> ></div> 
    You gave a rating of <span id="count">0</span> star(s) 
    <input type="hidden" class="form-control" id="rv_1" name="rv_1" value="2" /> 
     </div> 
     <div class="form-group"> 
    Review 2: <div id="stars_2" class="starrr" name ="rv_2" data-rating=<?php echo $rv_2 ?>></div> 
    You gave a rating of <span id="count_2">0</span> star(s) 
    <input type="hidden" class="form-control" id="rv_2" name="rv_2" value="4" /> 
     </div> 
    </div> 
     <input type="hidden" value="<?php $user_id ?>" name ="user_id_review"/> 
     <input type="hidden" value="<?php $company_id ?>" name ="company_id"/> 
     <div class="panel-footer"><button type="submit" class="btn btn-default" name="review">Save</button></div> 
    </div> 
    </form> 

答えて

1

を更新しました。

spanを読む/更新するときは、text()を使用してください(含めるHTMLが含まれていない限り)。

注:とvalue="<?php $company_id ?>にはechoコマンドがありません。

$(function() { 
 

 
    var $s1Input = $('#rv_1'); 
 
    var $s2Input = $('#rv_2'); 
 

 
    $('#stars_1').starrr({ 
 
    rating: $s1Input.val(), 
 
    change: function(e, value) { 
 
     $('#count').text(value); 
 
     $s1Input.val(value); 
 
    } 
 
    }); 
 

 
    $('#stars_2').starrr({ 
 
    rating: $s2Input.val(), 
 
    change: function(e, value) { 
 
     $('#count_2').text(value); 
 
     $s2Input.val(value); 
 
    } 
 
    }); 
 
});
.starrr { 
 
    display: inline-block; 
 
} 
 

 
.starrr a { 
 
    font-size: 16px; 
 
    padding: 0 1px; 
 
    cursor: pointer; 
 
    color: #FFD119; 
 
    text-decoration: none; 
 
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> 
 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://dobtco.github.io/starrr/dist/starrr.js"></script> 
 
<div class="form-group"> 
 
    Review 1: 
 
    <div id="stars_1" class="starrr"></div> 
 
    You gave a rating of <span id="count">0</span> star(s) 
 
    <input type="text" class="form-control" id="rv_1" name="rv_1" value="2" /> 
 
</div> 
 
<div class="form-group"> 
 
    Review 2: 
 
    <div id="stars_2" class="starrr"></div> 
 
    You gave a rating of <span id="count_2">0</span> star(s) 
 
    <input type="text" class="form-control" id="rv_2" name="rv_2" value="4" /> 
 
</div>

+0

それはあなたがスパンの値を設定すると同時に、非表示の入力フィールドに直接書き込む場合はスパンから値を読む必要はありません。 (私はデモで可視化しましたが、入力を 'type =" hidden "'に変更できます) –

+0

このコードスニペットを実行すると、inbuild StackOverflowコーディングセクションのエラーが表示されます。 – mimi

+0

'https'を使わずに' http:// dobtco.github.io/starrr/dist/starrr.js'を参照している可能性があります。私は私の答えを更新します.... –

関連する問題