简体   繁体   English

如何在JQuery 1.10.2 Ajax变量中获取隐藏的输入值?

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

I'm create Rating System using JavaScript when user select the star value set value in hidden input type. 当用户选择隐藏输入类型中的星值设置值时,我将使用JavaScript创建评级系统。 now i want to get that value in my ajax code and post to database. 现在我想在我的ajax代码中获取该值并发布到数据库。

I am try this way to get value of input 我试着通过这种方式获得输入值

 $(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);
                 }
                });
              });
           });

in actual file that code show 在代码显示的实际文件中

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

After Select the Star that code show in Resource 选择代码在资源中显示的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>

star value null show in result 星值null显示在结果中

Your query selector for the element seems correct, but I believe that your javascript data object is incorrect. 您的元素查询选择器似乎是正确的,但我相信您的JavaScript数据对象不正确。 You use blade syntax in an object, so you should most probably surround it with quotes. 您在对象中使用刀片语法,因此您最有可能用引号括起来。 Specifically I am talking about this two lines: 具体来说,我在谈论这两行:

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

And I believe that you are using this within a blade view and not in a .js file. 我相信您在刀片视图中使用它而不是在.js文件中。

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

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