주어진 옵션의 선택된 값만 인접한 위치에 저장하는 배열이 필요합니다. 아래 코드를 실행하면 모든 값이 같은 위치 (0)에 저장되고 나머지 배열은 비어 있습니다.배열의 값 밀어 넣기
<div>
<table id="myTable">
<tbody>
<tr>
<th>Robot By</th>
<th>Robot Name</th>
<th>Status</th>
<th>ADD</th>
<th>REMOVE</th>
</tr>
<tr id="col1">
<td>Google</td>
<td><em>googlebot</em></td>
<td>
<select class="myOption">
<option value="Same as Default">Same as Default</option>
<option value="Allowed">Allowed</option>
<option value="Refused">Refused</option>
</select></td>
<td>
<button type="button" class="addC">
Add
</button></td>
<td>
<button type="button" class="removeC">
Remove
</button></td>
</tr>
<tr id="col2">
<td>MSN Search</td>
<td><em>msnbot</em></td>
<td>
<select class="myOption">
<option value="Same as Default">Same as Default</option>
<option value="Allowed">Allowed</option>
<option value="Refused">Refused</option>
</select></td>
<td>
<button type="button" class="addC">
Add
</button></td>
<td>
<button type="button" class="removeC">
Remove
</button></td>
</tr>
<tr id="col3">
<td>Yahoo</td>
<td><em>yahoo-slurp</em></td>
<td>
<select class="myOption">
<option value="Same as Default">Same as Default</option>
<option value="Allowed">Allowed</option>
<option value="Refused">Refused</option>
</select></td>
<td>
<button type="button" class="addC">
Add
</button></td>
<td>
<button type="button" class="removeC">
Remove
</button></td>
</tr>
<tr id="col4">
<td>Ask/Teoma</td>
<td><em>teoma</em></td>
<td>
<select class="myOption">
<option value="Same as Default">Same as Default</option>
<option value="Allowed">Allowed</option>
<option value="Refused">Refused</option>
</select></td>
<td>
<button type="button" class="addC">
Add
</button></td>
<td>
<button type="button" class="removeC">
Remove
</button></td>
</tr>
</tbody>
</table>
</div>
<input type="button" value="Create Robots.txt" id="createFile">
<input type="reset" value="Clear All" id="clearAll">
다음 내 jQuery를 다음과 같습니다 :
$("#createFile").click(function() {
$('#myTable tr').find('td').each(function(index) {
$(this).find('.myOption').each(function(){
var myArray = [];
myArray.push($(".myOption option:selected").text());
});
각 옵션에 대해 새 배열을 만듭니다. – Vatev
변수는 JavaScript에서 둘러싼 함수의 로컬 변수입니다. – tadman