2011-11-10 3 views
0

각 드롭 다운 목록과 관련된 확인란이 있습니다. 사용자가 체크 박스를 클릭하면 체크 박스 값과 드롭 다운이 서버로 전송됩니다.jQuery .next()가 테이블에있을 때 작동하지 않습니다.

필자는 원래 각각을 ul로했지만 드롭 다운을 유지하기 위해 테이블로 변경했습니다. 내가 한 후에 다음 기능이 작동을 멈췄다. 요청의 var도 사라집니다. 값을 하드 코딩하면 백업을 보여줍니다.

$(document).delegate('.cbxAmenity', 'click', function() { 
    $("#dialogNotify").append(" Saving...").dialog(); 

    var thisAmenity = (this.id).replace("AmenityList_", ""); 

    $.ajax({ 
     url: 'ajax_url.php', 
     dataType: 'json', 
     data: { 
      ResortId: $("#id").val(), 
      action: 'save', 
      amenity: thisAmenity, 
      locality: $(this).next().val() // if i change to 'foo' it works 
     }, 
     success: function() { 
      $("#dialogNotify").dialog("close"); 
      ui.amenityDialog(); 
     }, 
     error: function() { 
      $("#dialogNotify").html("There was an error saving amenity"); 
     } 
    }); 

EDIT : 여기서

함수 인 다음 표는 ... 그 다른 Ajax 호출에 의해 생성 :

$("#dialogAmenity").html("<table id='amenityList'><tr><th>Amenity</th><th>Locality</th></tr>"); 
       for(var index=0; index<amenities.length; index++) { 
        $("#amenityList").append("<tr><td><input type='checkbox' name='AmenityList_"+ 
              amenities[index].AmenitiesID + "' id='AmenityList_"+ 
              amenities[index].AmenitiesID + "' class='cbxAmenity' /> "+ 
              amenities[index].Name + 
              "</td><td><select id='locality' name='locality'>"+ 
              "<option value='RESORT'>Resort</option>" + 
              "<option value='NEARBY'>Near by</option>" + 
              "</select></td></tr>"); 

        if (amenities[index].link == true) 
         $("#AmenityList_"+amenities[index].AmenitiesID).prop('checked', true); 

       } 
       $("#dialogAmenity").append("</table>"); 

EDIT 다시 : 여기 방화범

에서 HTML이고
<table id="amenityList"> 
    <tbody> 
    <tr> 
     <th>Amenity</th> 
     <th>Locality</th> 
    </tr> 
    <tr> 
     <td> 
     <input name="AmenityList_1" id="AmenityList_1" class="cbxAmenity" type="checkbox"> Indoor Pool 
     </td> 
     <td> 
     <select id="locality" name="locality"> 
      <option value="RESORT">Resort</option> 
      <option value="NEARBY">Near by</option> 
     </select> 
     </td> 
    </tr> 
    <tr> 
     <td> 
     <input name="AmenityList_2" id="AmenityList_2" class="cbxAmenity" type="checkbox"> Fitness Gym 
     </td> 
     <td> 
     <select id="locality" name="locality"> 
      <option value="RESORT">Resort</option> 
      <option value="NEARBY">Near by</option> 
     </select> 
     </td> 
    </tr> 
    <tr> 
     <td> 
     <input name="AmenityList_3" id="AmenityList_3" class="cbxAmenity" type="checkbox"> Beach Access 
     </td> 
     <td> 
     <select id="locality" name="locality"> 
      <option value="RESORT">Resort</option> 
      <option value="NEARBY">Near by</option> 
     </select> 
     </td> 
    </tr> 
    <tr> 
     <td> 
     <input name="AmenityList_4" id="AmenityList_4" class="cbxAmenity" type="checkbox"> Laundry Room 
     </td> 
     <td> 
     <select id="locality" name="locality"> 
      <option value="RESORT">Resort</option> 
      <option value="NEARBY">Near by</option> 
     </select> 
     </td> 
    </tr> 
    </tbody> 
</table> 
+2

'table'의 html을 게시 할 수 있습니까? – fncomp

+0

방금 ​​업데이트 됨 ... – guyfromfl

+0

테이블을 만드는 스크립트입니다. 마지막으로 생성 된 테이블의 예를 보여줄 수 있습니까? $ (this.id) .replace 함수 HTTP되지 않습니다 : : 나는 $의 방화 광이 말을 가지고 추가하면 그냥 테이블 코드 –

답변

2

요소 는 <td> 태그의 마지막 요소입니다. 따라서 next() 요소가 없습니다. 당신은 다음 <td><select> 요소를 찾기 위해

$(this).parent().next().find('select').val();

할 필요가있다.

+0

으로 편집합니다. 나는 td 컨테이너 내부에있는 것이 효과가 있었는지 궁금 해서요,하지만 그것을 어떻게 꺼내야할지 전혀 몰랐습니다 ... 감사합니다 – guyfromfl

+0

+1 Funnily, 내가이 질문에 답하기 전에, 나는 같은 문제를 가진 또 다른 질문을 보았습니다. –

2

테이블 생성 스크립트에 결함이 있습니다. 이 방법으로 닫기 태그를 추가 할 수 없습니다 $(...).append("</table>"). 클래스 cbxAminity

var html = "<table id='amenityList'><tr><th>Amenity</th><th>Locality</th></tr>"); 
    for(var index=0; index<amenities.length; index++) { 
    html += "<tr><td><input type='checkbox' name='AmenityList_"+ 
      amenities[index].AmenitiesID + "' id='AmenityList_"+ 
      amenities[index].AmenitiesID + "' class='cbxAmenity'" + 

      (amenities[index].link == true ? "checked='checked'" : "") + 

      "/> "+ 
      amenities[index].Name + 
      "</td><td><select id='locality' name='locality'>"+ 
      "<option value='RESORT'>Resort</option>" + 
      "<option value='NEARBY'>Near by</option>" + 
      "</select></td></tr>"); 

    } 
    html += "</table>"; 
    $("#dialogAmenity").html(html); 
+0

다시 업데이트 – guyfromfl

+0

. 내 첫 제안은 신경 쓰지 말고, 괄호를 제거하십시오. 대신, 제안 된 테이블 생성 코드를 살펴보십시오. –

+0

제안 사항을 변경했으나 아직 해당 var가 작성되지 않았습니다. – guyfromfl

관련 문제