2012-08-22 55 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”。

相关问题