2012-08-22 56 views
0

我目前正在使用JSP和jQuery手機一起使用Struts。問題是僅在頁面刷新一次後纔會調用javascript函數。腳本放置在數據角色「頁面」中。但問題仍然存在。目前我正在使用jQuery 1.0穩定版本。這裏是我的代碼..Javascript只能在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

你指的是哪一個javascript函數?我看到很多。沒有任何事件觸發器,函數如何被觸發? – Lowkase

+0

@Lokase:手機號碼驗證正在對提交,checkcount和textarea的按鍵上的goodchars調用。 getTemplate()用於填充下拉加載。當選擇任何選項時,將調用onchange事件以使用下拉列表的值填充textarea。 – Silver

回答

2

您需要將所有javascript放在頭部分之後,包括jQuery,但在調用jQuery移動之前。

你的頭文件應該類似於此(自定義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

:我試了一下。很嘈雜的工作,這裏是我在頭部添加的代碼。 它只在refresh時才調用javscript函數。我碰巧提到這個鏈接http://jquerymobile.com/test/docs/pages/page-scripting.html。它提到了通過Ajax加載的頁面。目前我沒有使用Ajax。如何禁用我的頁面的ajax調用? – Silver

+0

更新了我的答案。另外,我建議使用jQuery mobile 1.1.1 – adamdehaven

+0

Adam D:非常感謝你的回答。我解決了這個問題。填寫下拉菜單的功能並不像我寫過表格[0]一樣工作。我將其更改爲表單名稱並立即進行排序。此外,我確實將腳本放置在外部文件中,並將其從頭部鏈接到div data role page部分。它工作得很好,並從很多問題救了我..以及..) – Silver

0

爲我工作把數據阿賈克斯=在包含腳本的頁面的所有鏈接上都爲「false」。

相關問題