2011-03-20 134 views
0

假設我有一個如html和jquery這樣的表格html。用jquery選擇隱藏的元素值

我想找到保存點擊中的groupid的值。

我越來越不確定值

<table border="0" id="tableJoinList" cellpadding="4" cellspacing="5"> 
<tr><td colspan="3" align="left" style="padding-bottom:7px"> 
    <img src="../../Content/Images/doublelinegray.jpg" alt="doublelinegray.jpg" width="480px" height="4px" /> 
</td></tr> 
    @foreach (var item in Model.Groups) 
    { 
     <tr> 
      <td> 
       @Html.ActionLink("Join", "Join", new { id = item.GroupID }, new { @class = "Join" }) 
       @Html.Hidden("groupid", item.GroupID, new { @class = "groupid" }) 
      </td> 
      <td align="left"> 
       @item.GroupName 
      </td> 
      <td> 
       @item.DateAdded 
      </td> 
     </tr> 
    } 
<tr><td colspan="3" align="left"> 
    <img src="../../Content/Images/doublelinegrayreverse.jpg" alt="doublelinegrayreverse.jpg" width="480px" height="4px" /> 
</td></tr> 
</table> 

<script type="text/javascript"> 
$(function() { 
    $(".Join").live("click", function() { 
     var html = "<tr><td colspan='3'>Enter Password: &nbsp;&nbsp;<input type='password' class='pwd' />"; 
     html += "&nbsp;&nbsp;<input type='button' class='Save' value='Save' /></td></tr>"; 

     $(this).closest("tr").after(html); 

     $(".Save").live("click", function() { 
      alert($(this).siblings(".pwd").val()); 
      var json = { Password: $(this).siblings(".pwd").val(), GroupID: $(this).prev("tr").find(".groupid").val() }; 
      alert($(this).parent("tr").prev("tr").children(".groupid").val()); 
      return false; 
      $.ajax({ 
       url: "/Group/ConfirmPassword", 
       type: "POST", 
       dataType: 'json', 
       data: JSON.stringify(json), 
       contentType: "application/json; charset=utf-8", 
       success: function (result) { 
        if (result.Success == true) 
         window.location.href = result.Url; 
        else 
         alert(result.ErrorMessage); 
        return false; 
       } 
      }); 
     }); 
     return false; 
    }); 
}); 
</script> 
+1

我想你錯過了一些HTML ...保存按鈕在哪裏?這將有助於知道它是如何與行相關的,因爲你的問題可能出現在$(this).parent(「tr」)。prev(「tr」)。children(「。groupid」)'not targeting correctly 。哦,請分享最終的HTML,而不是發佈在那裏。 – Mottie 2011-03-20 04:21:52

+0

保存按鈕在Join中點擊javascript。 – Malcolm 2011-03-20 04:25:47

回答

1

好吧,我發現這個問題。使用.parent("tr")僅在DOM中上升一級,如果父級不是「tr」(在這種情況下實際是「td」),則寫​​入的方式是忽略其餘部分。另外.children()只下降一級,您需要使用.find()

所以您的警報應該是這樣的:

alert($(this).closest("tr").prev("tr").find(".groupid").val()); 

這是一個基本的demo