【发布时间】:2014-09-27 05:29:38
【问题描述】:
我有一个简单的表格,我正在尝试使用 jQuery 将单元格 .name 和 .date 中的文本值发送到表单中。该脚本运行良好,但我不知道如何仅在用户单击 Book 按钮的这一行中仅查找带有类的文本,例如 .name。
我的表结构:
<table id="myTable">
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>Place</th>
<th>Price</th>
<th>Hours</th>
<th>Book</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><a href="#" title="title1">Camp1</a></td> //send to form this value
<td class="date">10.09 - 15.09.2014 r.</td> //send to form this value
<td>Brighton</td>
<td>555 EURO</td>
<td>20</td>
<td class="book"><button>Book<button></td> //when user click on this button
</tr>
<tr>
<td class="name"><a href="#" title="title1">Camp2</a></td>
<td class="date">02.09 - 22.09.2014 r.</td>
<td>Brighton</td>
<td>432 EURO</td>
<td>20</td>
<td class="book"><button>Book<button></td>
</tr>
<tr>
<td class="name"><a href="#" title="title1">Camp3</a></td>
<td class="date">23.09 - 27.09.2014 r.</td>
<td>Brighton</td>
<td>123 EURO</td>
<td>20</td>
<td class="book"><button>Book<button></td>
</tr>
</tbody></table>
</tbody>
</table>
我的脚本:
<script>
$( "button" ).click(function() {
var text = $( '.name' ).text();
$( "#name" ).val( text );
var text = $( '.date' ).text();
$( "#date" ).val( text );
});
</script>
我的表格:
<fieldset id="contact_form" style="display:none;">
<div id="result"></div>
<label for="name"><span>Name</span>
<input type="text" name="name" id="name" placeholder="Enter Your Name" />
</label>
<label for="email"><span>Email Address</span>
<input type="email" name="email" id="email" placeholder="Enter Your Email" />
</label>
<label><span> </span>
<button class="submit_btn" id="submit_btn">Submit</button>
</label>
</fieldset>
<button c
lass="submit_btn" id="submit_btn">Submit</button>
</label>
</fieldset>
【问题讨论】:
标签: jquery