2014-12-04 68 views
-3

我想在js文件中創建一個表單。在運行時jQuery驗證

var form = '<form id="formForgetPass" action="#" method="POST" class="form-horizontal"><div class="form-group" style="margin-bottom: 0px !important;"><label for="textfield" class="control-label col-sm-2">E-posta</label><div class="col-sm-10"><input type="text" id="forgetEmail" name="email" class="form-control" /></div></div></form>'; 

而且我使用JquerValidation

是否有可能,我可以在運行時控制表單輸入?我還沒有想到。

它沒有下文

$("#formForgetPass").validate({ 
    rules: { 
     email: { 
      required: true, 
      email: true 
     } 
    } 
}); 
+0

你可以提供一些信息嗎?你有什麼嘗試?目標是什麼? – 2014-12-04 10:42:03

回答

0
<!DOCTYPE html> 
<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    var form = '<form id="formForgetPass" action="#" method="POST" class="form-horizontal"><div class="form-group" style="margin-bottom: 0px !important;"><label for="textfield" class="control-label col-sm-2">E-posta</label><div class="col-sm-10"><input type="text" id="forgetEmail" name="email" class="form-control" /></div></div></form>'; 

$("#MainDiv").append(form); 
alert($("#forgetEmail")); 

}); 
</script> 
</head> 
<body> 

<div id="MainDiv"> 
</div> 

</body> 
</html> 

U可以連接到div元素,讓元素正常工作。該警報將返回該對象。

+0

謝謝,但我附加形式進入正文,但我無法驗證表單輸入。 – 2014-12-04 12:18:49

0
<!DOCTYPE html> 
<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    var form = '<form id="formForgetPass" action="#" method="POST" class="form-horizontal"><div class="form-group" style="margin-bottom: 0px !important;"><label for="textfield" class="control-label col-sm-2">E- 

posta</label><div class="col-sm-10"><input type="text" id="forgetEmail" name="email" class="form-control" /></div></div></form>'; 

$("#MainDiv").append(form); 
}); 

function uivalidate() { 
    if ($("#forgetEmail").val() =='') { 
     alert(" Value Should not be empty !"); 
     return false; 
    } 
    alert('The value - ' + $("#forgetEmail").val()); 
} 
</script> 
</head> 
<body> 

<div id="MainDiv"> 
</div> 
<br> 
<button type="button" onClick="uivalidate();">Click Me!</button> 
</body> 
</html> 

    **Hi we can validate like this... this is question u r asking... give me the clarity** 
+0

運行時間添加了無法驗證的控件,如果您想手動驗證,請執行驗證.... – 2014-12-05 10:11:44