html 버튼을 클릭하면서 생성되는 양식이 있습니다.JQuery 유효성 검사가 작동하지 않습니다.
다음은 코드입니다.
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</head>
<script type="text/javascript">
function myFunction()
{
alert("Hello World!");
}
function createDoc()
{
var w=window.open('','','width=400,height=300');
w.document.open();w.document.write("<table width=350 height =200 <tr><td align=center>");
w.document.write("<table width=300 height =200 ><tr><td>");
w.document.write("<form class='commentform' action='hiii.php' method='GET'>");
w.document.write("<input type='hidden' name='pcode' value='00214'>");
w.document.write("<label>Name:</label></td><td><input type='text'id='pname' name='pname' value=''>");
w.document.write("</td></tr><tr><td>");
w.document.write("<label>Email Address:</label></td><td><input type='text' name='pemail' value=''/>");
w.document.write("</td></tr><tr><td>");
w.document.write("<label>Mobile number:</label></td><td><input type='text' name='pnumber' value=''/>");
w.document.write("</td></tr><tr><td align=center>");
w.document.write("<input type='submit' onclick='validate()' name='submit' value='Submit'/>");
w.document.write("</form>");w.document.write("</td></tr>");
w.document.write("</td></tr></table>");
w.document.write("</td></tr></table>");
w.focus();w.document.close();
}
function validate(){
var pname = $('#pname').val();
alert(pname);
}
</script>
<input type="button" value="Click to Connect" onclick="createDoc()">
</html>
클릭하여 연결 버튼 클릭하면 양식이 포함 된 팝업이 표시됩니다.
동적으로 생성 된 필드에 대한 유효성 검사를 적용하려고하지만 알림을받는 대신 양식이 직접 제출됩니다. 여기서 내가 잘못하고있는 것일 수 있습니다.
내가 명령은 당신이 당신의 유효성 검사를 실행하는 데 그 기본 동작을 중지 할 필요가 없도록
입력 상자에 일부 값을 입력 한 후 pname이 정의되지 않음을 나타냅니다. – Rohit
@Rohit 한 번 확인하고 코드를 업데이트했습니다. – som