2011-03-20 130 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