1

動的に生成されたフォームからレポートを生成しようとしています。フォームを介しXMLHttpRequestて適切にロードされますが、フォーム フィールドに対する検証は機能しません。eval()私はそれがロード時(のように)にのみ機能することを試しましeval(alert("hi")たが、domオブジェクトでは機能しません。スコープの問題を考えてください。フォーム フィールドは動的に生成されるため、データベース内の選択と可用性の役割に基づいて検証されます。

2 つのファイルのコードを以下に添付します。

<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 に答える