2011-02-10 10 views
1

복제 된 요소의 유효성 검사에 문제가 있습니다. 게시 된 코딩 양에 대해 사과하지만 처음부터 다시 넣을 수도 있다고 생각했습니다.jQuery 클론 유효성 검사

$('#btnAddDriver').live('click',function() { 
       var num  = $('.clonedInput').length; // how many "duplicatable" input fields we currently have 
       var newNum = new Number(num + 1);  // the numeric ID of the new input field being added 

       // create the new element via clone(), and manipulate it's ID using newNum value 
       var newElem = $('#addDriver1').clone(true).attr('id', 'addDriver' + newNum); 



       newElem.prepend("<div class='subhead1'>Add a driver "+newNum+"</div>"); 
       //newElem.find() 

       // insert the new element after the last "duplicatable" input field 
       $('#addDriver' + num).after(newElem); 
       //add the new ID to the inputs 
       newElem.find("*[id]").each(function() { 
        $(this).attr("id", $(this).attr("id") + newNum); 
        $(this).attr("name", $(this).attr("name") + newNum); 
        $(this).attr("value", $(this).attr("<?= $_SESSION['"+$(this).attr("name") + newNum+"']; ?>")); 
       }); 

       newElem.find("label").each(function() { 
       $(this).attr("for", $(this).attr("for")+ newNum); 
       }); 

이것은 예컨대 새로운 ID 및 이름의 클로닝 요소를 출력한다 :

<div id="addDriver1" class="clonedInput"> 
      <table width="100%" border="0" cellspacing="0" cellpadding="0" class="<?= $editDriver; ?>"> 


      <tr> 
      <td height="25" align="left" valign="top"><label for="driverReq" >Do you want to make this change or are 
you looking for a quote only?</label></td> 
      <td height="25" align="left" valign="top" colspan="2"><select name="driverReq" id="driverReq"> 
       <option selected="selected" value="">Please select</option> 
       <option value="Make Change" <? if ($_SESSION['driverReq']=="Make Change") { echo "selected";} ?> >Make Change</option> 
       <option value="Quote only" <? if ($_SESSION['driverReq']=="Quote only") { echo "selected";} ?>>Quote only</option> 
      </select></td> 
      </tr> 
      <tr> 
      <td height="5" colspan="3" ></td> 
      </tr> 
      <tr> 
      <td height="25" align="left" valign="top"><label for="driverDate" ;?>>What date would you like this change 
to be effective from? </label></td> 
      <td height="25" align="left" valign="top"><input name="driverDate" type="text" id="driverDate" style="width: 75px;" value="<?=$_SESSION['driverDate']; ?>" /></td> 
      <td height="25" align="left" valign="top">(DD/MM/YYYY) </td> 
      </tr> 
        <tr> 
      <td height="5" colspan="3" ></td> 
      </tr> 
</table></div> 

이 복제 스크립트 :

원래 DIV 샘플이고 "> 등

이 내가 노력하고있어 검증의 종류 :

 validateDriverReq2 = function(){  
     if(driverReq2.val()==""){ 
      $(this).addClass("error"); 
      driverReq2Info.html("Please confirm what you would like us to do with your request.<br>"); 
      $("#driverReq2Info").addClass("errorTop"); 
      //labelAddPC.addClass("labelError"); 
      $("label[for='driverReq2']").addClass("labelError"); 
      checkErrors(); 
      topErrorDriver(); 
      return false; 
     } 
     else{ 
      driverReq2.removeClass("error"); 
      driverReq2Info.html(""); 
      driverReq2Info.removeClass("errorTop"); 
      $("label[for='driverReq2']").removeClass("labelError"); 
      checkErrors(); 
      topErrorDriver(); 
      return true; 
     } 
    } 

    driverReq2.blur(validateDriverReq2); 
    driverReq2.change(validateDriverReq2); 

    validateDriverReq = function(){  

     if(driverReq.val()==""){ 
      $(this).addClass("error"); 
      driverReqInfo.html("Please confirm what you would like us to do with your request.<br>"); 
      $("#driverReqInfo").addClass("errorTop"); 
      //labelAddPC.addClass("labelError"); 
      $("label[for='driverReq']").addClass("labelError"); 
      checkErrors(); 
      topErrorDriver(); 
      return false; 
     } 
     else{ 
      driverReq.removeClass("error"); 
      driverReqInfo.html(""); 
      driverReqInfo.removeClass("errorTop"); 
      $("label[for='driverReq']").removeClass("labelError"); 
      checkErrors(); 
      topErrorDriver(); 
      return true; 
     } 
    } 

// } 

driverReq.blur(validateDriverReq); 
driverReq.change(validateDriverReq); 

사용 형태의 다른 영역에서이 코드는 잘 작동하지만,이 유효성 검사가 완전히 엉망이됩니다. 복제 된 driverReq2를 통해 이동하면 driverReq2 필드 자체에 대해 오류가 올바르게 발생하지만 원본 driverReq의 레이블이 활성화되어 표시됩니다. 그러면 driverReq2에서 옵션을 선택할 때 오류가 발생하지 않습니다. 변경 또는 흐림 효과가 발생할 때 자체를 제거합니다.

변수를 바인딩하려고 시도했으나 실패했습니다. 양식 이름 자체와 상위 항목 div (예 : # addDriver2). 내가 가가에게 완전히

감사

답변

-1

이 복잡하고 있지 많은 PPL 전체 코드를 읽어 가기 전에

내가 도와 내가 잘못 뭘하는지 조언 해 줄 수 있습니다 여기에 누군가를 바라고 있어요. 당신이 알아야 할 것은 다음과 같습니다.

복제 할 때 비슷한 기능을 가진 새 요소를 간단하게 만듭니다. 복제해야 할 요소에 ID가있는 경우 복제 할 때 id를 변경 한 다음 포함 된 live()을 사용하지 않는 한 복제 된 요소에 등록되지 않아야하는 이벤트를 알려야합니다 새로운 ID를 셀렉터로서 사용하거나 많은 요소에 같은 지시를하는 클래스를 사용한다.

희망이 도움이됩니다. 요소에 연결된 이벤트를 지우려면 라이브 전에 die()을 사용해야 할 수도 있습니다.