2011-03-08 254 views
0

我已经使用ajax(以任何基本教程显示的最简单的方式)在网页上更改我的主div,以便当我点击导航栏中的其他字段时,数据或表单显示在同一个div中。ajax和javascript表单验证

现在,我对ajax的理解显然存在问题,因为当我尝试使用javascript对使用ajax加载到我的页面的元素进行最简单的表单验证时,它不起作用。例如,一个复选框(未选中)和一个提交按钮加载了ajax。当我检查框并单击提交按钮只是为了查看复选框是否被选中,我什么都没有收到,或者我得到它没有被检查。

我试过同样的例子(复选框+提交)没有Ajax,它的工作原理很好。

在这种情况下,我错过了什么逻辑吗?

谢谢!

编辑:对不起,没有发布代码。 这是。我的Ajax代码:

function loadWholePage(url) 
{ 
    var y = document.getElementById("storage"); 
    //var x = document.getElementById("displayed"); 
    var x = document.getElementById("content"); 
    loadHTML(url, processHTML, x, y); 
} 

function loadHTML(url, fun, storage, param) 
    { 
var xhr = createXHR(); 
xhr.onreadystatechange=function() 
{ 
    if(xhr.readyState == 4) // The request is complete 
    { 
     //if(xhr.status == 200) 
     { 
      storage.innerHTML = getBody(xhr.responseText); 

      fun(storage, param); 
     } 
    } 
}; 


xhr.open("GET", url, true); 
xhr.send(null); 
    } 

function getBody(content) 
{ 
test = content.toLowerCase(); // to eliminate case sensitivity 
var x = test.indexOf("<body"); 
if(x == -1) return ""; 

x = test.indexOf(">", x); 
if(x == -1) return ""; 

var y = test.lastIndexOf("</body>"); 
if(y == -1) y = test.lastIndexOf("</html>"); 
if(y == -1) y = content.length; // If no HTML then just grab everything till end 

return content.slice(x + 1, y); 
} 

HTML文件我加载使用Ajax:

<html> 
<body> 
<form> 
<input type="checkbox" name="values" value="" id="chk3" /> 
<select name="select3" class="test" style="width:7em;" id="select3"> 
<option VALUE="">0-100</option> 
<option VALUE="/tags/">100-200</option> 
<option VALUE="/"> 200-300</option> 
<option VALUE="/"> 300-400</option> 
<option VALUE="/"> 400-500</option> 
</select> 
<input type="button" class="submit" id="testbutton" style="width:12em;" value="submit" onClick="testIt();"> 
</form>        
</body> 
</html> 

我使用加载:

<li onClick='setSelectedListElement(this);'> 
<a onclick="loadWholePage('test.html');">TEST 
</a> 
</li> 

而真正原始脚本,应检查是否复选框被选中:)

<script type="text/javascript"> 
function testIt(){ 
var x=document.getElementById("chk3").checked; 
alert(x); 
} 
</script> 
+1

你能发表一些代码吗? – Neal

+0

没有代码,谁知道,但可能必须做的:元素被不正确地添加(不被DOM识别),使用jQuery选择器在添加新内容之前运行并且不反映更新的DOM ... –

+0

我编辑过我原来的帖子,希望这不是问题。谢谢! – Newman1510

回答

0

开加载你的页面,右键单击 - >查看源代码,并检查在运行时生成的控件的ID。 尝试使用控件的这些ID(复选框等)并检查验证是否被触发。