请记住,我是客户端脚本的新手,正在学习我的方式......
我需要创建一个包含表格数据的页面,并让用户能够单击特定链接以带来模式弹出窗口并提交请求。
我将此表单放入 DIV 并使用 jQuery 显示它,没问题。对于“表格”,我对如何以最佳方式连接点击和数据感到两难。我倾向于用类来做,但是我的 HTML 看起来是这样的:
<ItemTemplate>
<tr>
<td class="tripId"><%# DataBinder.Eval(Container.DataItem, "TripId")%></td>
<td class="pickupDate"><%# DataBinder.Eval(Container.DataItem, "PickupDate", "{0:MM/dd/yy}")%></td>
<td class="from"><%# DataBinder.Eval(Container.DataItem, "FromCity")%>, <%# DataBinder.Eval(Container.DataItem, "FromState")%></td>
<td class="deliveryDate"><%# DataBinder.Eval(Container.DataItem, "DeliveryDate", "{0:MM/dd/yy}")%></td>
<td class="to"><%# DataBinder.Eval(Container.DataItem, "ToCity")%>, <%# DataBinder.Eval(Container.DataItem, "ToState")%></td>
<td class="weight"><%# DataBinder.Eval(Container.DataItem, "Weight")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "LoadedMiles")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "RequiredEquipment")%></td>
<td><a href="#" class="inquireLink"><strong>Inquire now</strong></a></td>
</tr>
</ItemTemplate>
在javascript中,我像这样挂钩所有href:
// Hookup link events to handle modal popup
$(".inquireLink").on("click", function (event) {
event.preventDefault();
inquireAboutTrip($(this));
});
function inquireAboutTrip($link) {
// Link located on <TR> so we can traverse up and then into each cell to get data
}
因此,我可以从链接遍历到 TR,然后进入每个 CLASS 以查找 TD 内的数据。然后我可以根据需要在表单上使用这些数据。有两个问题:VS2012 抱怨所有这些类都是未定义的(当然它们不在 CSS 上)。另一个问题是,对于每一行,我都有这个标记的重复。我希望它更干净。我知道如果我使用 jQuery,我可以依赖 TR 中的 TD 顺序。代码变得更脆弱,但 HTML 更少。
解决此类问题的常用方法是什么?