簡體   English   中英

如何在JQuery 1.10.2 Ajax變量中獲取隱藏的輸入值?

[英]How to Get Hidden Input Value in JQuery 1.10.2 Ajax variable?

當用戶選擇隱藏輸入類型中的星值設置值時,我將使用JavaScript創建評級系統。 現在我想在我的ajax代碼中獲取該值並發布到數據庫。

我試着通過這種方式獲得輸入值

 $(document).ready(function(){
          $('#ajaxSubmit').click(function(e){
              e.preventDefault();
              $.ajaxSetup({
                 headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                 }
             });
              $.ajax({
                 url: "/review",
                 type: 'POST',
                 data: {
                    description: $('textarea[name=description]').val(),
                    star: $('input:hidden[name=score]').val(),
                    user_id: {{Auth::user()->id}},
                    product_id: {{$product->id}}
                 },
                 dataType: "JSON",
                 success: function(result){
                    console.log(result);
                 }
                });
              });
           });

在代碼顯示的實際文件中

<div class="star big" id="rating" data-score="0"></div>

選擇代碼在資源中顯示的Star之后

<div class="star big" id="rating" data-score="0" style="cursor: pointer; width: 80px;"><img src="assets/images/star-big-on.png" alt="1" title="bad">
<img src="assets/images/star-big-on.png" alt="2" title="poor">
<img src="assets/images/star-big-on.png" alt="3" title="regular">
<img src="assets/images/star-big-on.png" alt="4" title="good">
<img src="assets/images/star-big-on.png" alt="5" title="gorgeous">
<input id="star" type="hidden" name="score" value="5"></div>

星值null顯示在結果中

您的元素查詢選擇器似乎是正確的,但我相信您的JavaScript數據對象不正確。 您在對象中使用刀片語法,因此您最有可能用引號括起來。 具體來說,我在談論這兩行:

user_id: "{{Auth::user()->id}}",
product_id: "{{$product->id}}"

我相信您在刀片視圖中使用它而不是在.js文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM