2010-08-19 5 views
0

다음 코드는 shareform newform.aspx의 일부 필드를 확인하는 코드입니다. 셰어 포인트 양식을 제출하지 않으려면 PreSaveItem 기본 함수에 거짓 진술을 반환해야합니다.jQuery를 사용하여 양식의 드롭 다운에서 선택한 항목에 따라 다른 필드의 유효성을 검사 하시겠습니까?

확인 :

나는 아이템 레벨라는 드롭 다운 목록에서 선택한 내용에 따라 서로 다른 요소를 유효성을 검사 할
//bind a change event to all controls to validate 
    $("input[title=Target Date],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective],select[title=Strategic 

Priority]").change(function(){ 
     checkControls() 
    }); 

    //the change event function - check the status of each control 
    function checkControls(){ 

    //set a variable to count the number of valid controls 
    var controlsPassed = 0; 

    //set up a selector to pick .each() of the target controls 
    $("input[title=Target Date],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective],select[title=Strategic 

Priority]").each(function(){ 

     //if the control value is not zero AND is not zero-length 
     var txt = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl00_UserField_hiddenSpanData').val(); 
     var val = $(this).val(); 
     if($(this).is(':hidden') || (val != 0 && val.length != 0 && txt.length != 0)) { 

      //add one to the counter 
      controlsPassed += 1; 
     } 

     }); 

    //call the PreSaveItem function and pass the true/false statement of 5 valid controls 

return (controlsPassed == 5) 

    } 
     function PreSaveItem() { 
      return checkControls() 
    } 

. 내가 그냥 경우의에 유효성 검사 코드를 척 쉬운 것입니다하지만 작동하지 않는 생각

$("select[title='Item Level']").change(function() { 
     var itemLevel = $(this).val();  
     if (itemLevel == "Strategic Objective") { 


     alert(itemLevel); 
     } 
     if (itemLevel == "Strategic Priority") { 


     alert(itemLevel); 
     } 
     if (itemLevel == "Milestone Action") { 


     alert(itemLevel); 
     } 
     if (itemLevel == "Performance Measure") { 


     alert(itemLevel); 
     } 

     }); 

:

내가 가진 아이템 레벨의 값을 얻는다. 예를 들어

:

 $("select[title='Item Level']").change(function() { 
     var itemLevel = $(this).val();  
     if (itemLevel == "Strategic Objective") { 


     alert(itemLevel); 
     } 
     if (itemLevel == "Strategic Priority") { 


     alert(itemLevel); 
     } 
     if (itemLevel == "Milestone Action") { 
     //bind a change event to all controls to validate 
    $("input[title=Target Date],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective],select[title=Strategic 

Priority]").change(function(){ 
     checkControls() 
    }); 

    //the change event function - check the status of each control 
    function checkControls(){ 

    //set a variable to count the number of valid controls 
    var controlsPassed = 0; 

    //set up a selector to pick .each() of the target controls 
    $("input[title=Target Date],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective],select[title=Strategic 

Priority]").each(function(){ 

     //if the control value is not zero AND is not zero-length 
     var txt = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl00_UserField_hiddenSpanData').val(); 
     var val = $(this).val(); 
     if($(this).is(':hidden') || (val != 0 && val.length != 0 && txt.length != 0)) { 

      //add one to the counter 
      controlsPassed += 1; 
     } 

     }); 

    //call the PreSaveItem function and pass the true/false statement of 5 valid controls 

return (controlsPassed == 5) 

    } 
     function PreSaveItem() { 
      return checkControls() 
    } 


     alert(itemLevel); 
     } 
     if (itemLevel == "Performance Measure") { 


     alert(itemLevel); 
     } 

     }); 

및 항목 수준 항목 전략적 목적에

몇 가지 다른 요소를 확인합니다. 어떤 아이디어?

미리 감사드립니다.

편집 : 카사 블랑카의 도움으로 작업 코드 :

function checkControls() { 
    var itemLevel = $("select[title='Item Level']").val(); 
    switch (itemLevel) { 
    case 'Strategic Objective': 

    var controlsPassed = 0; 

    $("input[id$=UserField_hiddenSpanData]").each(function(){ 

     var txt = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_hiddenSpanData').val(); 
     var val = $(this).val(); 
     if(val != 0 && val.length != 0 && txt.length != 0) { 

      //add one to the counter 
      controlsPassed += 1; 
     } 

     }); 
     return (controlsPassed == 1) 

    case 'Milestone Action': 

     var controlsPassed = 0; 

    $("input[title=Target Date],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective],select[title=Strategic 

Priority]").each(function(){ 

     var txt = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_hiddenSpanData').val(); 
     var val = $(this).val(); 
     if($(this).is(':hidden') || (val != 0 && val.length != 0 && txt.length != 0)) { 

      //add one to the counter 
      controlsPassed += 1; 
     } 

     }); 
     return (controlsPassed == 5) 

    case 'Performance Measure': 

     var controlsPassed = 0; 

    $("select[title=Strategic Objective],select[title=Strategic Priority]").each(function(){ 

     var val = $(this).val(); 
     if(val != 0 && val.length != 0) { 

      //add one to the counter 
      controlsPassed += 1; 
     } 

     }); 
    return (controlsPassed == 2) 

case 'Strategic Priority': 

     var controlsPassed = 0; 

    $("input[title=Target Date],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective]").each(function(){ 

     var txt = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_hiddenSpanData').val(); 
     var val = $(this).val(); 
     if($(this).is(':hidden') || (val != 0 && val.length != 0 && txt.length != 0)) { 

      //add one to the counter 
      controlsPassed += 1; 
     } 

     }); 
    return (controlsPassed == 4) 

    } 
} 

function PreSaveItem() 
    { 
      return checkControls() 
    } 

답변

0

좋아, 이것이 내가 현재 스크립트에서 볼 수있는 문제이다 : 당신이 선택하는, 그래서 만약 이벤트 처리기를 바인딩, 영구적 "마일스톤 조치" 한 번만 다른 옵션으로 전환해도 원래 선택한 내용의 유효성은 계속 유지됩니다.

모든 change 핸들러를 제거하고 현재 조건을 checkControls에 직접 추가하여 현재 선택 사항을 기반으로 필요한 요소의 유효성을 검사 할 수 있습니다. 나는 또한 청소기의 이후 switchif의 교체했습니다

function checkControls() { 
    var itemLevel = $("select[title='Item Level']").val(); 
    switch (itemLevel) { 
    case 'Strategic Objective': 
     // Perform validation for this item level 
     return result; // true or false 

    case 'Milestone Action': 
     // Perform validation for this item level 
     return result; // true or false 

    // etc... 
    } 
} 

업데이트 : "마일스톤 조치"를 선택하면 경우 텍스트 필드가 비어 있지 않은 경우 예를 들어, 확인하고 싶은 다음 case 'Milestone Action':에서, 당신은 이런 식으로 뭔가를 추가 :

if ($('something').val().length > 0) 
    return true; 
else 
    return false; 

편집 : 양식 코드에 액세스 할 수있는 경우 (나는 F 아니에요 SharePoint에 익숙한 사용자)에게는 양식 요소에 ID를 할당해야하므로 긴 선택기를 사용하는 대신 $('#itemLevel')과 같은 참조 할 수 있습니다.

+0

안녕하세요. 카사 블랑카 님, 고맙습니다.하지만 코드를 어떻게 변경해야할지 모르겠습니다. – Peter

+0

몇 가지 예를 들어 답을 업데이트했습니다. 희망이 도움이됩니다. – casablanca

+0

고마워, 지금 받았어. 셀렉터에 대해서는 폼 코드에 액세스 할 수 없지만 각 요소에 jQuery 클래스를 추가 할 수 있습니다. 나는 또한 내가 가지고있는 최종 코드로 내 질문을 업데이트한다. – Peter

관련 문제