ajax,当它具有不同的值时,我将获得相同的值 [英] ajax,I am getting same value when its has different value

查看:47
本文介绍了ajax,当它具有不同的值时,我将获得相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我单击了不同的值,但是console.log给了我相同的值.在此处输入图片描述

I am clicking on different value but console.log give me same value. enter image description here

我也会去具有不同的 value属性的html代码具有不同的值,但是,它给了我属性的第一个值.在此处输入图片描述

i also going html code which has different value attribute has different value but, its give me first value of attribute. enter image description here

实际上,ajax有2个选项.这就是我累了.

actually, there 2 options for ajax. here is what i have tired.

<script type="text/javascript">

    $(function(){
        $('.categoryList').click(function(){
          
          var cat_id = $(this).attr('value');
          var url = "http://localhost:8000/api/getSubcategory/"+cat_id;
          $.ajax({
              type: "GET",
              url: url,
              dataType: "JSON",
              success: function(res)
              {
                var html = "";
                $.each(res, function (key, value) {
                     html += "<option  value="+key+">"+value+" </option>";
                });
                 $('#subcategory').html($(html).addClass('subcategoryList'));

              }
          });
        });

    });
$(document).ready(function() {
    $('#subcategory').click(function() {

       var  subcat_id = $( this ).find( '.subcategoryList' ).attr('value');
          console.log(subcat_id);
          var url = "/api/getSubcategorytwo/"+subcat_id;
          $.ajax({
              type: "GET",
              url: url,
              dataType: "JSON",
              success: function(res)
              {
                
                var html = "";
                $.each(res, function (key, value) {
                     html += "<option value="+key+">"+value+"</option>";
                });
                $("#subcategorytwo").html(html);

              }
          });
        });

    });
</script>

                   <div class="col-md-4" >
              
                        @foreach($categories as $category)
                        <option  class="categoryList" value="{{$category->id}}">{{$category->category}}</option>
                        @endforeach
                    
                    </div>
                 
                        <div class="col-md-4 ">
                        <a  name="subcategory" id="subcategory" >
                         </a>
                        </div>
                  
                       <div class="col-md-4">
                        <a name="subcategorytwo" id="subcategorytwo" >
                          <option value=""></option>
                        </a>
                        </div>
           

enter code here

推荐答案

指定的html代码都是错误的.您可以使用< a/> 定位标记,也可以使用< select>< option></option></select> 选择标记.

The html code specified is all wrong. You either go with <a /> anchor tag or <select><option></option> </select> select tag.

如果您更喜欢 anchor 标记,则可以获得点击功能,但是在html中,您最终会看到一堆锚点

If you prefer anchor tag then you can get the click functionality, but in the html you will end up with a long list of anchors

如果选择 select 标记,则可以调用on change事件并获取选定的值.您可以使用以下任何一种方式

If you choose the select tag, then you can invoke the on change event and get the selected value. You can use either of the following

有关更改功能

使用jquery更改事件处理程序

这篇关于ajax,当它具有不同的值时,我将获得相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆