1

我正在尝试通过动态生成的表单生成报告。通过XMLHttpRequest表单加载良好,但对表单字段的验证将不起作用。我已经尝试过eval()它仅在加载时间(如eval(alert("hi"))期间有效,但不适用于 dom 对象,请考虑一些范围问题。表单字段是动态生成的,因此它的验证基于数据库中的选择和可用性角色。

下面附上两个文件的代码

<script>

function showUser(str)
{

if (window.XMLHttpRequest)
{
 xmlhttp=new XMLHttpRequest();
 }

 xmlhttp.onreadystatechange=function()
 {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
    var s= xmlhttp.responseText;

    parseScript1(s);
    parseScript(s);
   }

  }

    xmlhttp.open("GET","test2.php",true);
    xmlhttp.send();
  }


   function parseScript1(_source) {
var source = _source;
var scripts = new Array();

 // Strip out tags
 while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
    var s = source.indexOf("<script");
    var s_e = source.indexOf(">", s);
    var e = source.indexOf("</script", s);
    var e_e = source.indexOf(">", e);

    // Add to scripts array
    scripts.push(source.substring(s_e+1, e));
    // Strip from source
    source = source.substring(0, s) + source.substring(e_e+1);
  }
var k = "<script> "+ scripts +"<\/script>";

    document.getElementById("txtHint1").innerHTML=k ;



 // Return the cleaned source
 return source;
  }




    function parseScript(_source) {
     var source = _source;
      var scripts = new Array();

 // Strip out tags
 while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
    var s = source.indexOf("<script");
    var s_e = source.indexOf(">", s);
    var e = source.indexOf("</script", s);
    var e_e = source.indexOf(">", e);

    // Add to scripts array
    scripts.push(source.substring(s_e+1, e));
    // Strip from source
    source = source.substring(0, s) + source.substring(e_e+1);
}

 document.getElementById("txtHint").innerHTML=source;


// Return the cleaned source
return source;
}


function valid()
  {
  eval(validate());
  }




      </script>
<div id="txtHint1"><b>javascript will appear here</b></div>
<form>
<div id="nons1" >
<select id="nope1" name="users" onchange="showUser(this)">
<option value="">Select a option:</option>
<option value="1">not working here</option>

</select>
</div>
</form>

<div id="txtHint"><b>Select the value .</b></div>

test2.php

    echo "<p>This form works fine singly  not with xmlhttprequest";

    echo'<form name="frm" method="post" action="test2.php" onsubmit="return(eval(validate()));">';
    echo '<input value="" name="kfrm19" type="text">';
    echo '<input name="save" value="submit" type="submit"></form>';

echo '<script>
function validate(){
if( document.frm.kfrm19.value.trim()=="")
   {
     alert( "If you can see this message .its working..." );
     document.frm.kfrm19.focus() ;
     return false;
   }
}


</script>';



 ?>
4

1 回答 1

0

尝试这个:

function validate(){
if(document.frm.kfrm19.value.length == 0)
   {
     alert("If you can see this message .its working...");
     document.frm.kfrm19.focus();
     return false;
   }
}
于 2013-09-05T19:03:26.787 回答