2014-09-19 28 views
0

我有表:ASP.NET MVC的JQuery最接近HiddenFor VAL

<table class="table table-bordered"> 
     @for (var i = 0; i < Model.Count; i++) 
     { 
      <tr> 
       <td width="25px"> 
        @if (!Model[i].Letter.Equals(letter)) 
        { 
         @Html.DisplayTextFor(m => Model[i].Letter); 

        } 
       </td> 
       <td width="45px"> 
        <div class="button-edit"></div> 

       </td> 
       <td> 
        @Html.HiddenFor(m => Model[i].Student.Id, new { @class = "mojaklasa" }) 

       </td> 

       <td> 
        ... 
       </td> 
       <td> 
        ... 
       </td> 
       <td> 
        ... 
       </td> 
      </tr> 

     } 
    </table> 

,我怎麼能得到最接近hiddenfor值按鈕? 我想:

$(document).ready(function() { 
      $('.button-edit').each(function() { 
       $(this).mouseenter(function() { 
        $(this).fadeTo(20, 1).css('cursor', 'pointer'); 
       }); 
       $(this).mouseleave(function() { 
        $(this).fadeTo(20, 0.5); 
       }); 
       $(this).click(function() { 

        var id; 
        $(this).closest('.mojaklasa').val(id); 
        alert(id); 

       }); 
      }); 

,並用$(本)。接下來.... 不過如果我點擊這個按鈕,我沒有在警告框東西。 我甚至把類放進去,但我可以從中得到這個文本。

回答

0

您需要找到父tr,然後嘗試查找輸入。

$(document).ready(function() { 
     $('.button-edit').each(function() { 
      $(this).mouseenter(function() { 
       $(this).fadeTo(20, 1).css('cursor', 'pointer'); 
      }); 
      $(this).mouseleave(function() { 
       $(this).fadeTo(20, 0.5); 
      }); 
      $(this).click(function() { 
       var id; 
       $(this).closest("tr").find('.mojaklasa').val(id); 
       alert(id); 

      }); 
     }); 
0

試試這個

$(this).closest('tr').find('.mojaklasa').val(id); 
0

你需要做的搜索相對於母公司的項目點擊的,但你不應該在each連接多個事件。這是毫無意義的,因爲jQuery爲您管理多個處理程序。

$(function() { 
    $('.button-edit').mouseenter(function() { 
     $(this).fadeTo(20, 1).css('cursor', 'pointer'); 
    }).mouseleave(function() { 
     $(this).fadeTo(20, 0.5); 
    }).click(function() { 
     var id = $(this).closest("tr").find('.mojaklasa').val(); 
     alert(id); 
    }); 
}); 

這將3個處理程序連接到所有元件與.button-edit類。您還需要使用從val()返回的值,而不是使用參數(因爲它設置隱藏值)調用val