<table border="1" bordercolor="black" cellspacing="0" align="center" cellpadding="5"> <thead> <tr> <td> 产品编号 </td> <td> 产品名称 </td> <td> 产品描述 </td> <td> 父类 </td> <td> 增加子类别 </td> <td> 编辑 </td> <td> 删除 </td> </tr> </thead> <c:forEach items="${pageBean.queryResult.resultList}" var="productType"> <tr class="productName"> <td class="productTypeId"> ${productType.id } </td> <td class="productTypeName"> ${productType.name } </td> <td> ${productType.note } </td> <td class="parentTypeId"> <c:if test="${!empty productType.parentType}">${productType.parentType.name}</c:if> </td> <td> <a href="#dialog" name="addChildProductType">增加</a> </td> <td class="td"> <input name="#update" type="button" value="编辑" /> </td> <td> <a name="deleteProductType" href="controller/product/productType!deleteProductType.action?productType.id=${productType.id} ">删除</a> </td> </tr> </c:forEach> </table>
在点击“编辑”按钮之后,我想获取“编辑”按钮相对应的那行:
<td class="parentTypeId"> <c:if test="${!empty productType.parentType}">${productType.parentType.name}</c:if> </td>
里面的html文本值;我的写法是这样的:
var parentTypeName = $(this).parent().parent() .children(".parentTypeId").html(); alert(parentTypeName);
但是提示框显示的是null(我调试的时候是选择那些有值的),我觉得我的问题应该是这段javascript有问题。请朋友赐教。我的想法是这样的:$(this)得到的是
<input name="#update" type="button" value="编辑" />
$(this).parent()得到的是
<td class="td"> <input name="#update" type="button" value="编辑" /> </td>
$(this).parent.parent()得到的是
<tr class="productName"> <td class="productTypeId"> ${productType.id } </td> <td class="productTypeName"> ${productType.name } </td> <td> ${productType.note } </td> <td class="parentTypeId"> <c:if test="${!empty productType.parentType}">${productType.parentType.name}</c:if> </td> <td> <a href="#dialog" name="addChildProductType">增加</a> </td> <td class="td"> <input name="#update" type="button" value="编辑" /> </td> <td> <a name="deleteProductType" href="controller/product/productType!deleteProductType.action?productType.id=${productType.id} ">删除</a> </td> </tr>
最后
$(this).parent().parent() .children(".parentTypeId").html();
获得的是
<td class="parentTypeId"> <c:if test="${!empty productType.parentType}">${productType.parentType.name}</c:if> </td>
的节点值。这么想哪里有错?
解决这个问题的完整代码:
<div id="table"> <table border="1" bordercolor="black" cellspacing="0" align="center" cellpadding="5"> <thead> <tr> <td> </td> <td> 产品编号 </td> <td> 产品名称 </td> <td> 产品描述 </td> <td> 父类名称 </td> <td> 增加子类别 </td> <td> 编辑 </td> <td> 删除 </td> </tr> </thead> <c:forEach items="${pageBean.queryResult.resultList}" var="productType"> <tr class="productName"> <td class="parentTypeId"> <c:if test="${!empty productType.parentType}"> <input type="hidden" value="${productType.parentType.id}" /> </c:if> </td> <td class="productTypeId"> ${productType.id } </td> <td class="productTypeName"> ${productType.name } </td> <td> ${productType.note } </td> <td class="parentTypeName"> <c:if test="${!empty productType.parentType}">${productType.parentType.name}</c:if> </td> <td> <input name="#add" type="button" value="添加"></input> </td> <td> <input name="#update" type="button" value="编辑" /> </td> <td> <input name="#delete" type="button" value="删除" /> <%--<a name="deleteProductType" href="controller/product/productType!deleteProductType.action?productType.id=${productType.id} ">删除</a> --%> </td> </tr> </c:forEach> </table> </div>
<div id="update" class="window"> <img class="close" alt="close" src="/sports/images/greenAcross.gif"> <form name="updateProductType" action="controller/product/productType!updateProductType.action" method="post"> <table> <caption> 添加产品子类型 </caption> <tr> <td> 产品父类型: </td> <td> <select name="select"> </select> </td> </tr> <tr> <td> 产品类型名称: </td> <td> <input id="productTypeName" type="text" name="productType.name"> </td> </tr> <tr> <td> 产品类型备注: </td> <td> <input id="productTypeNote" type="text" name="productType.note"> </td> </tr> <tr> <td> <br /> <input type="submit" name="" value="submit"> </td> <td> <br /> <input type="reset" name="" value="reset"> </td> </tr> </table> </form> </div>
对应的jquery代码:
$('input[name=#update]').click(function(e) { var parentTypeId = $(this).parent().parent().children(".parentTypeId") .children().attr("value"); var currentElement = $(this).parent().parent() .children(".parentTypeName"); var parentTypeName = currentElement.html(); var productTypeName = currentElement.prev().html(); var productTypeNote = currentElement.prev().prev().html(); // $("#productTypeName").attr("value", "cccccc"); // $("#productTypeNote").attr("value", "dddddd"); alert((productTypeName).toString()); $("#productTypeName").attr("value", productTypeName); $("#productTypeNote").attr("value", "eeeeeee"); alert(productTypeName); var id = $(this).attr('name'); location(id); $(id).fadeIn(2000); $.ajax({ type : "post", dataType : "json", url : "controller/product/productType!getIdAndName.action", success : function(data, textStatus) { var select = $("select[name=select]"); var option = ""; // var parentTypeName = $(this).parent().parent() // .children(".parentTypeId").html(); // alert(parentTypeName); // var productType = eval("(" + data + ")"); option += "<option value=\"" + parentTypeId + "\">" + parentTypeName + "</option>"; for (var i = 0; i < data.result; i++) { option += "<option value=\"" + data.rows[i].productId + "\">" + data.rows[i].productTypeName + "</option>"; } select.html(option); }, complete : function(XMLHttpRequest, textStatus) { // HideLoading(); // alert("complete"); }, error : function() { alert("error"); } }); });但是新问题来了:上面代码的这句并不能正确赋值,截图在附件