2012-08-22 3 views
0

저는 현재 Struts와 함께 jquery 모바일과 함께 JSP로 작업하고 있습니다. 문제는 자바 스크립트 함수가 페이지가 한 번 새로 고쳐진 후에 만 ​​호출된다는 것입니다. 스크립트는 데이터 역할 '페이지'내에 배치됩니다. 그러나 문제는 여전히 지속됩니다. 현재 jQuery 1.0 안정 버전을 사용하고 있습니다. 여기jQuery 모바일에서 페이지 새로 고침에만 작동하는 자바

<body> 
    <div data-role="page" id="webtosms"> 

     <script language="javascript"> 

     function phonenumlen(){   //Mobile no validation 
      var numlen = mobileno.value.length; 
      //alert(numlen); 
      if(numlen==0){ 
       alert('Mobile Number cannot be left blank'); 
       return false; 
      } 

      else if(numlen<10) 
      { 
       alert('Mobile number cannot be less than 10 digits'); 
       return false; 
      } 
      else 
      { 
       //alert('true'); 
       return true; 
      } 


     } 

     function goodchars(e,goods){ // restrict users from entering letters in the mobile number textbox 
      var key, keychar; 
      key = getkey(e); 
      if (key == null) return true; 
      // get character 
      keychar = String.fromCharCode(key); 
      keychar = keychar.toLowerCase(); 
      goods = goods.toLowerCase(); 
      // check goodkeys 
      if (goods.indexOf(keychar) != -1) 
       return true; 
      // control keys 
      if (key==null || key==0 || key==8 || key==9 || key==13 || key==27) 
       return true; 
      return false; 
     } 

     function getkey(e) 
     { 
      if (window.event) 
       return window.event.keyCode; 
      else if (e) 
       return e.which; 
      else 
       return null; 
     } 

     langId = 'EN'; 
     messageLen = 299; 
     message = ""; 

     function checkCount() { 
      //alert('function called'); 

      if(document.webtosms.message.value.length <= messageLen) { 
       message = document.webtosms.message.value; 
       document.webtosms.charcount.value = (messageLen - document.webtosms.message.value.length); 
      }else { 
       document.webtosms.message.value = message; 
      } 
     } 

     function getTemplate(){ // code to populate the drop down and display in the textarea 


      var where_is_mytool=document.forms[0].cboTemplate.value; 
      var mytool_array=where_is_mytool.split("~"); 

      //alert(where_is_mytool); 
       alert(mytool_array); 
      window.document.forms[0].smsa.value=mytool_array[0]; 
      window.document.forms[0].tmplid1.value=mytool_array[1]; 
      window.document.forms[0].title2.value=mytool_array[1]; 
      window.document.forms[0].hidlang.value=mytool_array[2]; 


      window.document.forms[0].hidcreatedbyval.value=mytool_array[5]; 


     } 
    </script> 
    </div> 

위의 코드는 페이지가 갱신되면 절대적으로 잘 작동 .. 내 코드입니다. 이미로드 된 페이지를 다시로드하고 싶지 않습니다. 도와주세요.

+1

어떤 자바 스크립트 기능을 언급하고 있습니까? 나는 많은 것을 본다. 이벤트 트리거없이 함수가 어떻게 시작됩니까? – Lowkase

+0

@Lokase : textarea의 keypress에서 submit, checkcount 및 goodchars에서 모바일 번호 유효성 검사가 호출됩니다. getTemplate()은 드롭 다운 온로드를 채우기위한 것입니다. 옵션을 선택하면 onchange 이벤트가 호출되어 텍스트 영역에 드롭 다운의 값을 채 웁니다. – Silver

답변

2

jQuery를 포함시킨 후 모든 javascript를 head 섹션에 넣어야하지만 jQuery mobile을 호출하기 전에

머리 파일이 (사용자 정의 두 번째 파일에서 JS)에 유사합니다 :

<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script> 
<script type="text/javascript" src="http://www.example.com/path-to-file/custom-javascript.js"></script> 
<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script> 

UPDATE 페이지의 링크를

, 속성 data-ajax="false"을 추가합니다. 당신이 아약스 탐색 사이트 전체 사용 중지하려면 사용자 지정 JS 파일에 다음 코드를 넣어 : 여기

$(document).bind("mobileinit", function(){ 
    $.extend( $.mobile , { 
    ajaxEnabled: false 
    }); 
}); 

이 문서에 대한 링크입니다 : 나를 위해 http://jquerymobile.com/demos/1.1.1/docs/api/globalconfig.html

+0

: 나는 그것을 시도했다. 슬프게도 din 작업은 여기에 머리 부분에 추가 한 코드이다. 새로 고침시 javscript 함수를 호출합니다.이 링크를 참조했습니다. http://jquerymobile.com/test/docs/pages/page-scripting.html. Ajax를 통해로드되는 페이지를 말합니다. 현재 Ajax를 사용하지 않습니다. 내 페이지에 대한 ajax 호출을 비활성화하려면 어떻게합니까? – Silver

+0

내 대답이 업데이트되었습니다. 또한 jQuery 모바일 1.1.1을 사용하는 것이 좋습니다. – adamdehaven

+0

Adam D : 답변을 주셔서 대단히 감사합니다. 나는 그 문제를 알아 냈다. 폼을 작성 했으므로 드롭 다운을 채우는 기능이 작동하지 않았습니다. 양식 이름으로 변경하고 즉시 정렬했습니다. 또한 스크립트를 외부 파일에 배치하고 머리글과 div 데이터 역할 = 페이지 섹션을 연결했습니다. 그것은 절대적으로 잘 작동하고뿐만 아니라 많은 문제에서 나를 구해 주었다;) – Silver

0

데이터 - 아약스 넣어 일했다 = 스크립트가 포함 된 페이지에 대한 모든 링크에 "false"가 표시됩니다.

관련 문제