我在带有表格的模态框中创建了一个表单。单击表行时,它会在父页面上填充表单。这工作得很好,但是当我点击另一个表行时,我无法更改值。因为我正在使用ID,所以它只接受第一个表行中的值。我愿意换班,但我试过这个并没有帮助。
任何人都可以帮助我。代码如下。
带表格的模态表格
<div id="modal_form" title="Address Search">
<form id="modal_form">
<ul>
<li><label for="name">Search by street description</label>
<input type="text" name="street_description" id="street_description" />
<input type="button" id="search_button" class="form_button" value="Search"></li>
</ul>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table-data">
<tbody><tr>
<td width="200px"><a href="#">Street</a></td>
<td width="200px"><a href="#">Suburb</a></td>
<td width="200px"><a href="#">City</a></td>
</tr>
<tr>
<td id="address_street">Harambee Road</td>
<td id="address_suburb">Onerai </td>
<td id="address_city">Onerai Rural</td>
</tr>
<tr>
<td id="address_street">Hutchinson Road</td>
<td id="address_suburb">Onerai </td>
<td id="address_city">Onerai Rural</td>
</tr>
<tr>
<td id="address_street">Kauri Road</td>
<td id="address_suburb">Onerai </td>
<td id="address_city">Onerai Rural</td>
</tr>
</tbody></table><!-- /table#table-data -->
</form>
Javascript(这是我需要区分表行的地方)
<script type="text/javascript">
$(document).ready(function() {
$('#table-data tr').click(function () {
$('#street_name').val( $('#address_street').text() )
$('#suburb').val( $('#address_suburb').text() )
$('#city').val( $('#address_city').text() )})
});
</script>
父表单,填写表单字段
<form id="profile">
<ul>
<li><label for="street_number">Street Number</label><input id="street_number" type="text" placeholder="Street Number" name="street_number" ><input type="button" class="form_button" id="find_address" value="Find Address"></li>
<li><label for="street_name">Street Name</label><input id="street_name" type="text" placeholder="Street Name" name="street_name" disabled="disabled" ></li>
<li><label for="suburb">Suburb</label><input id="suburb" type="text" placeholder="Suburb" name="suburb" disabled="disabled" ></li>
<li><label for="city">City</label><input id="city" type="text" placeholder="City" name="city" disabled="disabled" ></li>
<li><input type="submit" id="submit" value="Save"></li>
</ul>
</form>
感谢您提供任何帮助,欢呼声
答案 0 :(得分:2)
ID必须是唯一的,因此这不起作用:
<td id="address_street">Harambee Road</td>
...
<td id="address_street">Hutchinson Road</td>
我会给每个行一个ID,并让所有列使用相同的类:
<tr id="row1">
<td class="address_street">Harambee Road</td>
<td class="address_suburb">Onerai </td>
<td class="address_city">Onerai Rural</td>
</tr>
<tr id="row2">
<td class="address_street">Hutchinson Road</td>
<td class="address_suburb">Onerai </td>
<td class="address_city">Onerai Rural</td>
</tr>
<tr id="row3">
<td class="address_street">Kauri Road</td>
<td class="address_suburb">Onerai </td>
<td class="address_city">Onerai Rural</td>
</tr>
您的JavaScript会改变为:
<script type="text/javascript">
$(document).ready(function() {
$('#table-data tr').click(function () {
var curRowId = $(this).attr("id");
$('#street_name').val( $('#' + curRowId + ' td.address_street').text() );
$('#suburb').val( $('#' + curRowId + ' td.address_suburb').text() );
$('#city').val( $('#' + curRowId + ' td.address_city').text() );
});
});
</script>