【问题标题】:How to get id and value for each field? JQuery如何获取每个字段的 id 和 value? jQuery
【发布时间】:2013-06-04 11:04:59
【问题描述】:

亲爱的stackoverflowers,

我有一个关于获取特定字段的 ID 和值的问题。现在,我有以下代码:

<div class="category_edit_form">
    <input type = "text" name = "category_edit_text" class = "category_edit_text" id = "'.$category->category_id.'">
    <input type = "button" class="category_edit_button" value = "Wijzig">
</div>

这是 foreach 循环的输出。因此,显示的每个类别都包含一个文本字段和一个用于更改名称的按钮。这是我的 jQuery:

jQuery(".category_edit_button").click( function() {
    jQuery.ajax({
        async: false,
        url: nieuwsbrief.updatecategory,
        type: 'POST',
        data: { category_id: jQuery('.category_edit_text').prop('id'), category_name: jQuery('.category_edit_text').val()},
        success: function(data) {
            alert(data);
        }
    });
}); 

这是要提醒的数据:

echo $_POST['category_id']."en de naam is: ".$_POST['category_name'];

但是每次我点击任何按钮时,我只能看到第一个字段的值和 id。因此,当我单击第二个文本字段的按钮时,会弹出第一个字段的 id 和值,而不是第二个文本字段的 id 和值。

我该如何解决这个问题?

【问题讨论】:

    标签: jquery button input textfield


    【解决方案1】:

    用途:

    jQuery(".category_edit_button").click( function() {
    
        jQuery.ajax({
                async: false,
                url: nieuwsbrief.updatecategory,
                type: 'POST',
                data: { category_id: jQuery(this).parent().find('.category_edit_text').prop('id'), category_name: jQuery('.category_edit_text').val()},
                    success: function(data) {
                            alert(data);
                    }
        });
    
    }); 
    

    【讨论】:

      【解决方案2】:

      您需要确定选择器的范围以获取.edit_category_text

      jQuery(".category_edit_button").click( function() {
          var $parent = $(this).parent();
          jQuery.ajax({
                  async: false,
                  url: nieuwsbrief.updatecategory,
                  type: 'POST',
                  data: { category_id: jQuery('.category_edit_text',$parent).prop('id'), category_name: jQuery('.category_edit_text',$parent).val()},
                      success: function(data) {
                              alert(data);
                      }
          });
      
      }); 
      

      【讨论】:

        【解决方案3】:

        用那个代替:

        jQuery('.category_edit_text').prop('id')
        

        这段代码:

        jQuery(this).prev('.category_edit_text').prop('id')
        

        prevAll(),如果不是直接上一个元素

        【讨论】:

          猜你喜欢
          • 2013-07-25
          • 2019-07-28
          • 1970-01-01
          • 1970-01-01
          • 2013-03-11
          • 1970-01-01
          • 2023-03-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多