您好我是javaScript / jquery的新手。我有以下由框架自动生成的HTML代码。
<tr class="alternate-row">
<td>17 July, 2012</td>
<td>AM Shipment</td>
<td>Generated</td>
<td>
<form method="post" action="" id="ListTruckSheets_o_7" class="basic-form" onsubmit="javascript:submitFormDisableSubmits(this)" name="ListTruckSheets_o_7">//I have the name of this form.
<input type="hidden" name="shipmentId" value="10310" id="ListTruckSheets_shipmentId_o_7">
<input type="hidden" name="Report" id="truckSheet_o_7" onclick="javascript:setSelectedRoute(this, 10310);">
<input type="hidden" name="abstractReport" id="routeAbstract_o_7" onclick="javascript:setSelectedRoute(this, 10310);">
<input type="hidden" name="milkDeliverySchedule" id="deliverySchedule_o_7" onclick="javascript:setSelectedRoute(this, 10310);">
<input type="hidden" name="cancelShipment" id="ListTruckSheets_cancelShipment_o_7">
</form>
<td>
<a class="buttontext" href="/myportal/control/KVGenerateTruckSheet.txt?shipmentId=10310&reportTypeFlag=trucksheet" target="_blank" onclick="javascript:setSelectedRoute(this, 10310);" title="Report">Truck Sheet Report</a>
</td>
<td>
<a class="buttontext" href="/myportal/control/KVGenerateTruckSheet.txt?shipmentId=10310&reportTypeFlag=abstract" target="_blank" onclick="javascript:setSelectedRoute(this, 10310);" title="Abstract Report">Route Abstract Report</a>
</td>
<td>
<a class="buttontext" href="/myportal/control/MilkDeliveryScheduleReport.txt?hipmentId=10310&reportTypeFlag=milkDeliverySchedule" target="_blank" onclick="javascript:setSelectedRoute(this, 10310);" title="Milk Delivery Schedule">Delivery Schedule</a>
</td>
<td>
<form method="post" action="/myportal/control/cancelLMSShipment" target="_blank" onsubmit="javascript:submitFormDisableSubmits(this)" name="ListTruckSheets_o_7_0_o_cancelShipment">
<input name="shipmentId" value="10310" type="hidden">
</form>
<a class="buttontext" href="javascript:document.ListTruckSheets_o_7_0_o_cancelShipment.submit()">Cancel</a>
</td>
</tr>
使用onclick我调用了一个javaScript函数,它获取url并为其添加一个参数。我有表单的名称或ID('ListTruckSheets_o_7')。使用表单名称或Id我需要找到父tr并标识我想要的正确的td并将参数设置为其url。这是我目前正在搞乱的代码。
function setSelectedRoute(existingUrl, shipmentId) {
var form = $("input[value=" + shipmentId + "]").parent(); //formName: ListTruckSheets_o_7
var routeId = form.find("option:selected").text();
var resultUrl = existingUrl + "&facilityId=" + routeId;
$('.buttontext').attr('href', resultUrl); // this is setting resultUrl to all the .buttontext class elements but I need to set resultUrl to only specific selected td.
}
所以,可能是,我需要找到父tr并返回到特定的td并将resultUrl设置为它。我不知道该怎么做。我希望我对自己的问题很清楚。谢谢..
答案 0 :(得分:0)
您可以测试一下:
form
.closest('tr') // go parent tr of form
.find('.specific_selected_td') // find td, here specific_selected_td is
// assumed class to selected td
// you may have something different
.find('a') // find anchor tag within that specific td
.attr('href', resultUrl); // set href
更改
$('.buttontext').attr('href', resultUrl);
到
$(existingUrl).attr('href', resultUrl);
此处,existingUrl
表示点击的a
。