2015-06-04 56 views
0

我試圖從js文件訪問數據,並在html文本框中獲取值,我無法獲得結果。這裏samle.js文件和文件jsonhtml.html傳遞值使用json的文本框

{ 
 
"var1":"1"; 
 
"var2":"2"; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<html> 
 
<head> 
 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
 
<script> 
 
    $(document).ready(function() { 
 
     $.ajaxSetup({cache: false}); 
 
     setInterval(function() { 
 
      $.getJSON("sample.js", function (data) { 
 
       if (data.var1) { 
 
        $('#C1-Cycle').val(data.var1); 
 
       } 
 
       if (data.var2) { 
 
        $('#C2-Cycle').val(data.var2); 
 
       } 
 
      }); 
 
     }, 1000); 
 
    }); 
 
})(jQuery); 
 
</script> 
 
</head> 
 
<body> 
 
<div> 
 
<input type="text" id="C1-Cycle"> 
 
</div> 
 
<hr> 
 
<div> 
 
<input type="text" id="C2-Cycle-Cycle"> 
 
</div> 
 
</body 
 
</html>

+1

把警報'typeof運算data'權的getJSON語句後,看看你會得到什麼和JSON必須是'{ 「VAR1」: 「1」, 「VAR2」: 「2」} '。 – lshettyl

+0

爲什麼你連接到jquery兩次? – Vishnu

+4

首先它不是有效的json。有效的json是:{ 「var1」:「1」, 「var2」:「2」 } –

回答

3

使用這個腳本:

$(document).ready(function() { 
     $.ajaxSetup({cache: false}); 
     var winterval=setInterval(function() { 

      $.getJSON("sample.js", function (result) { 
       var data=$.parseJSON(result); 

       if (data.var1) { 
        $('#C1-Cycle').val(data.var1); 
       } 
       if (data.var2) { 
        $('#C2-Cycle-Cycle').val(data.var2); 
       } 
      }); 

     }, 1000); 
    }); 

但要確保在你的sample.js文件中的數據就像是:

{「var1」:「1」,「var2」:「2」}

例子è爲:https://jsfiddle.net/5881pnqn/2/

+0

你的小提琴文件工作,仍然設置層間函數不工作,當我嘗試訪問sample.js中的數據,它不檢索結果..注意:我的sample.js文件只有一行'{「var1」:「1」,「var2」:「2」}' – RJ10

+0

check現在的jsfiddle:https://jsfiddle.net/5881pnqn/2/檢查控制檯間隔工作 –

+0

https://jsfiddle.net/5881pnqn/2/ –

0
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    <title></title> 
    <script> 
     function validate() { 
      debugger; 

      var name = document.myForm.name.value; 
      if (name == "" || name == null) { 
       document.getElementById("span1").innerHTML = "Please enter the Name"; 
       document.myForm.name.focus(); 
       document.getElementById("name").style.border = "3px solid red"; 
       return false; 
      } 
      else { 
       document.getElementById("span1").innerHTML = ""; 
       document.getElementById("name").style.border = "3px solid green"; 
      } 

      var age = document.myForm.age.value; 
      var ageFormat = /^(([1][8-9])|([2-5][0-9])|(6[0]))$/gm; 

      if (age == "" || age == null) { 
       document.getElementById("span2").innerHTML = "Please provide Age"; 
       document.myForm.age.focus(); 
       document.getElementById("age").style.border = "3px solid red"; 
       return false; 
      } 
      else if (!ageFormat.test(age)) { 
       document.getElementById("span2").innerHTML = "Age can't be leass than 18 and greater than 60"; 
       document.myForm.age.focus(); 
       document.getElementById("age").style.border = "3px solid red"; 
       return false; 
      } 
      else { 
       document.getElementById("span2").innerHTML = ""; 
       document.getElementById("age").style.border = "3px solid green"; 
      } 

      var password = document.myForm.password.value; 
      if (document.myForm.password.length < 6) { 
       alert("Error: Password must contain at least six characters!"); 
       document.myForm.password.focus(); 
       document.getElementById("password").style.border = "3px solid red"; 
       return false; 
      } 
      re = /[0-9]/g; 
      if (!re.test(password)) { 
       alert("Error: password must contain at least one number!"); 
       document.myForm.password.focus(); 
       document.getElementById("password").style.border = "3px solid red"; 
       return false; 
      } 
      re = /[a-z]/g; 
      if (!re.test(password)) { 
       alert("Error: password must contain at least one lowercase letter (a-z)!"); 
       document.myForm.password.focus(); 
       document.getElementById("password").style.border = "3px solid red"; 
       return false; 
      } 
      re = /[A-Z]/g; 
      if (!re.test(password)) { 
       alert("Error: password must contain at least one uppercase letter!"); 
       document.myForm.password.focus(); 
       document.getElementById("password").style.border = "3px solid red"; 
       return false; 
      } 
      re = /[$&+,:;[email protected]#|'<>.^*()%!-]/g; 
      if (!re.test(password)) { 
       alert("Error: password must contain at least one special character!"); 
       document.myForm.password.focus(); 
       document.getElementById("password").style.border = "3px solid red"; 
       return false; 
      } 
      else { 
       document.getElementById("span3").innerHTML = ""; 
       document.getElementById("password").style.border = "3px solid green"; 

       createJSON(); 
      } 
     } 
     function createJSON() { 
      debugger; 
      var myData = $("#myTableData"); 
      var jsonData= JSON.stringify(myData.serializeArray()); 
      document.getElementById("span4").innerHTML = jsonData; 
      console.log(jsonData); 


     } 
     function restrictCharacters(evt) { 

      evt = (evt) ? evt : window.event; 
      var charCode = (evt.which) ? evt.which : evt.keyCode; 
      if (((charCode >= '65') && (charCode <= '90')) || ((charCode >= '97') && (charCode <= '122')) || (charCode == '32')) { 
       return true; 
      } 
      else { 
       return false; 
      } 
     } 



     function clearForm() { 
      document.getElementById("name").innerHTML = ""; 
      document.getElementById("age").innerHTML = ""; 
      document.getElementById("password").innerHTML = ""; 
     } 
    </script> 
</head> 

<body> 
    <div id="myForm"> 
     <h2>Registration Form</h2> 
     <form id="myTableData" name="myForm" onsubmit="return(validate());"> 

      <table> 

       <tr> 
        <th>Name</th> 
        <td><input type="text" name="name" placeholder="Name" id="name" class="information" onkeypress="return restrictCharacters(event);" /></td> 
        <td><span id="span1"></span></td> 
       </tr> 

       <tr> 
        <th>Age</th> 
        <td> 
         <input type="text" name="age" id="age" class="information" onkeypress="return event.charCode === 0 || /\d/.test(String.fromCharCode(event.charCode));" placeholder="Age" /></td> 
        <td><span id="span2"></span></td> 
       </tr> 

       <tr> 
        <th>Password</th> 
        <td> 
         <input type="password" name="password" id="password" class="information" placeholder="Password" /></td> 
        <td><span id="span3"></span></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td> 
         <input type="button" name="button" value="Proceed" onclick="validate();" /> 

        </td> 
       </tr> 
       <tr> 
        <td> 
         <span id="span4"></span> 
        </td> 
       </tr> 

      </table> 
     </form> 
    </div> 


</body> 
</html>