2016-01-06 43 views
0

循環通過一些值。我現在有8段。從同一元素獲取值

@foreach (var child in element.Childs) 
{ 
    <p> 
     <input type="hidden" name="id" value="@child.Id" class="form-control"> 
     <input type="text" name="Name" value="@child.Name" class="form-control"> 
    </p> 
} 

當我按下輸入我想打電話給一個頁面,但我需要的ID和名稱。

$(document).ready(function() { 
    $('p input').keydown(function(e) { 
     if (e.keyCode == 13) { 
      e.preventDefault(); 

      $.ajax({ 
       type: 'POST', 
       url: '/Category/Update', 
       data: { 
        'id': ID_VALUE, 
        'name': $(this).val() 
       } 
      }); 
     } 
    }); 
}); 

當我按在文本框中輸入,我如何得到正確的ID?所以我需要從相同的段落中獲得id的值。

+0

使用'id'屬性:'this.id'。請注意,儘管您的元素沒有'id'屬性。 –

+0

您的示例中沒有任何元素具有ID屬性。 – j08691

+0

'$(this).find('#id')。val()'會這樣嗎? –

回答

0

您輸入欄改成這樣:

<input type="text" name="Name" id="@child.id" value="@child.Name" class="form-control"> 

,然後在你的Ajax調用使用:

 $.ajax({ 
      type: 'POST', 
      url: '/Category/Update', 
      data: { 
       'id': this.id, 
       'name': $(this).val() 
      } 
     }); 

,你可以刪除你的隱藏字段(除非它還有其他功能!)