script type="text/javascript">
var n=1;
$(document).ready(function(){
$(":input[type=button][name=more]").bind("click",function(){
n++;
var $br = $("br />");
var $file = $("input type='file' name='myfiles' />");
var $url= $("input type='input' name='url“+n+”' />");
var $button = $("input type='button' value='刪除' class='btn btn-warning'>");
$(this).after($url).after($br).after($file).after($br).after($button).after($br);
$button.bind("click",function(){
$br.remove();
$file.remove();
$url.remove();
$button.remove();
});
});
});
//驗證
function check(f) {
if (f.myfiles) {
if (f.myfiles.length) { //多組
for (var i = 0; i < f.myfiles.length; i++) {
f (f.myfiles[i].value == '') { alert('請選擇文件!'); f.myfiles[i].focus(); return false; }
if (f.url[i].value!=''&&!/^https?:\/\/.+?\.com$/i.test(f.url[i].value)) {alert('URL必須以http://或者hppts://開頭,.com結尾!'); f.url[i].select(); return false; }} }
else { //只有一組
if (f.myfiles.value == '') { alert('請選擇文件!'); f.myfiles.focus(); return false; }
if (f.url.value!=''&&!/^https?:\/\/.+?\.com$/i.test(f.url.value)){ alert('URL必須以http://或者hppts://開頭,.com結尾!'); f.url.select(); return false;
} } } return false }
/script>
form action="<%=request.getContextPath()%>/add.do"
method="POST" enctype="multipart/form-data"
onsubmit="return check(this)">
input type="file" name="myfiles"/>
網站地址:<input type="input" name="url" />
input type="button" name="more"value="添加" class="btn btn-info" />
tr><td><input type="submit" value="確認"class="btn btn-success" />
http://ask.csdn.net/questions/178479