Javascript 使用jQuery验证表单提交
我有一个表单,我通过克隆它来添加行。我的表单中也有一个下拉值。我想在我的表单上添加两个验证Javascript 使用jQuery验证表单提交,javascript,jquery,html,Javascript,Jquery,Html,我有一个表单,我通过克隆它来添加行。我的表单中也有一个下拉值。我想在我的表单上添加两个验证 如果用户没有填写所有字段,则他将无法添加另一行(请记住,如果下拉值设置为默认值,则不应允许用户添加另一行) 在表单提交时,我需要相同的验证,如果有任何字段留空或选择了下拉列表的默认值,则用户应该无法提交表单 我已经尝试过了,但是它仍然包含很多bug,因为我是jQuery新手 var regex = /^([a-zA-Z0-9 _-]+)$/; var cindex = 0; var qui
var regex = /^([a-zA-Z0-9 _-]+)$/;
var cindex = 0;
var quicklink = '' ;
$(document).on('click','.Buttons', function(addrow) {
var count = $('table tr:last input:text').filter((_,el) => el.value.trim() == "").length;
if(count || !$('.id_100 option[value=description]').attr('selected','selected')){
{
alert("Please fill the current row");
return false;
}}
var $tr = $('#dataTable tbody tr:last');
var $clone = $tr.clone(true);
$('.DeleteButton').prop('disabled', false);
cindex++;
$clone.find('input:text').val('').attr('disabled', true);
$clone.find('input:button').attr('disabled', true);
$clone.attr('id', 'id'+(cindex) ); //update row id if required
//update ids of elements in row
$clone.find("*").each(function() {
var id = this.id || "";
if(id != ""){
var match = id.match(regex) || [];
if (match.length == 2) {
this.id = this.name + (cindex);
}
}
});
$tr.after($clone);
});
//-----------------------------------------------------------------------
function disableField(e)
{ var Count = $('#dataTable tr').length;
if (Count == 2){
$(e).closest('tr').find("input").not('.DeleteButton').prop('disabled', false);
}else{
$(e).closest('tr').find("input").prop('disabled', false);
}}
//--------------------------------------------------------------------
$(document).on("click", '.DeleteButton', function() {
$(this).closest("tr").remove();
var Count1 = $('#dataTable tr').length;
if (Count1 == 2){
$('.DeleteButton').prop('disabled', true);
}
});
//---------------------------------------------------------------------.
function fnOnSubmit() {
var count = $('table tr:last input:text').filter((_,el) => el.value.trim() == "").length;
if(count || !$('.id_100 option[value=description]').attr('selected','selected')){
{
alert("Please fill all the fields");
return false;
}}
var flag='';
$('#dataTable input:not([type=button]),#dataTable select').each(function () {
flag = flag + (this.value) + '~';
quicklink = flag;
});
document.frmmain.quicklink.value = quicklink;
SendTxnRequest('02','QWC');
}
HTML
var regex=/^([a-zA-Z0-9_-]+)$/;
var-cindex=0;
var-quicklink='';
$(文档).on('click','Buttons',函数(addrow){
var count=$('table tr:last input:text').filter((2;,el)=>el.value.trim()==“”).length;
if(count | |!$('.id_100 option[value=description]').attr('selected','selected')){
{
警告(“请填写当前行”);
返回false;
}}
var$tr=$(“#数据表tbody tr:last”);
var$clone=$tr.clone(true);
$('.DeleteButton').prop('disabled',false);
cindex++;
$clone.find('input:text').val('').attr('disabled',true);
$clone.find('input:button').attr('disabled',true);
$clone.attr('id','id'+(cindex));//如果需要,更新行id
//更新行中元素的ID
$clone.find(“*”).each(函数(){
var id=this.id | |“”;
if(id!=”“){
var match=id.match(正则表达式)| |[];
if(match.length==2){
this.id=this.name+(cindex);
}
}
});
$tr.after($clone);
});
//-----------------------------------------------------------------------
函数禁用字段(e)
{var Count=$('#dataTable tr')。长度;
如果(计数=2){
$(e).最近('tr').find('input').not('DeleteButton').prop('disabled',false));
}否则{
$(e).最近的('tr').find('input').prop('disabled',false);
}}
//--------------------------------------------------------------------
$(文档)。在(“单击“,”.DeleteButton',函数()上){
$(this).tr.remove();
var Count1=$('#dataTable tr')。长度;
如果(Count1==2){
$('.DeleteButton').prop('disabled',true);
}
});
//---------------------------------------------------------------------.
函数fnOnSubmit(){
var count=$('table tr:last input:text').filter((2;,el)=>el.value.trim()==“”).length;
if(count | |!$('.id_100 option[value=description]').attr('selected','selected')){
{
警告(“请填写所有字段”);
返回false;
}}
var标志=“”;
$('#数据表输入:非([type=button]),#数据表选择')。每个(函数(){
flag=flag+(this.value)+'~';
快速链接=标志;
});
document.frmmain.quicklink.value=quicklink;
SendTxnRequest('02','QWC');
}
链接
描述
形象
统一资源定位地址
挑选
向导
最新报价
你能为你的问题创建一个JSFIDLE吗?我已经在代码段中添加了我的代码。现在,所有的字段都被禁用了,所以你不能输入任何内容,你为什么要这样做?还有,你为什么要在需要
时使用
?我的
就在
里面