Validation 验证输入字段,使其在jQuery mobile中唯一且不重复

Validation 验证输入字段,使其在jQuery mobile中唯一且不重复,validation,jquery-mobile,unique,Validation,Jquery Mobile,Unique,在我的jQuery移动应用程序中,我有一个报警服务,用户输入每天的报警数量,一旦用户输入报警数量,比如说3,应用程序将创建3个时间输入字段,当用户单击保存按钮保存数据时,我需要验证所有创建的输入时间字段,以便输入的时间不等于当前时间,唯一性此时没有报警,我已通过以下代码解决了此问题,但问题是当用户输入唯一时间“非重复时间”时,我将exist值从true更改为false,但它没有输入并执行此条件 否则,如果(exist==false&&$(this).attr('value')!=currentT

在我的jQuery移动应用程序中,我有一个报警服务,用户输入每天的报警数量,一旦用户输入报警数量,比如说3,应用程序将创建3个时间输入字段,当用户单击保存按钮保存数据时,我需要验证所有创建的输入时间字段,以便输入的时间不等于当前时间,唯一性此时没有报警,我已通过以下代码解决了此问题,但问题是当用户输入唯一时间“非重复时间”时,我将exist值从true更改为false,但它没有输入并执行此条件

否则,如果(exist==false&&$(this).attr('value')!=currentTime)直接“更新”exist值,我需要再次单击“保存”按钮,以使代码知道exist值已更改,并继续在DB中保存报警数据

我怎样才能解决这个问题?请帮帮我

<body>

<div data-role="page"   id="Alarm">
<div data-role="header" ></div>
<div data-role="content" >


 <form>

   <input type="number"  id="AlarmTimes"/>
   <label for="AlarmTimes" > Number of alarms Per day </lable>
   <div   id="timeFields"  style="display:none;" >

   </div>
   <div>
     <input type="button" id="SaveBtn" value="Save Alarm "/></div>
    </form>
 </div>
 </div>

</body>

每天的警报数量
javascript

var alarm_TimesNum;
var exist = true;

var ExistAlarmTimesArr = [];



$('#AlarmTimes').on('keyup' , function(){

    alarm_TimesNum = $('#AlarmTimes').attr('value');

    var timeFields = $('#timeFields');   

    if(timeFields.children().length != 0 )
    {   
       timeFields.empty();                                
       timeFields.hide();
    } 


    for( var i=1 ; i<= alarm_TimesNum ;i++)
    {

       timeFields.append($( '<div class="ui-grid-a" ><div class="ui-block-a"> <input  type="time" name="alarmTime"    class="AlarmTime" /></div>' 
    +'<div class="ui-block-b" ><label for=" alarmTime"  ><font  size="5px"  style="font- weight: normal;" class="Time_label"> Time:: </font></label></div></div>'));


       timeFields.trigger('create');
    }


    timeFields.show();


 });


 db.transaction(getExistAlarmsTime,transError,transSuccess);

 $('#SaveBtn').on('click',function(){


     $('.AlarmTime').each(function(i){

     if( $(this).attr('value') == currentTime || $.trim(   $(this).attr('value')).length ==0 )
         {  
        $('.Time_label').each(function(j){  if(j==i){        $(this).addClass('missing');}  });
            if(i==0){ alert('Enter time for alarm 1 '); }
    else if(i==1){alert('Enter time for alarm 2 ');  }
    else if(i==2){alert('Enter time for alarm 3 ');   }

      }
          else if( $(this).attr('value') != currentTime && exist ==   true )
      {   
          for( var k=0;k<ExistAlarmTimesArr.length;k++)
               {  
                   if( $(this).attr('value') == ExistAlarmTimesArr[k])
           {
                    $('.Time_label').each(function(j){  if(j==i){        $(this).addClass('missing');}  });

                        if(i==0){alert( 'Enter Another Time  for alarm 1 you have another alarm at this time '); }
                else if(i==1){ alert( 'Enter Another Time  for alarm 2 you have another alarm at this time ');  }
                else if(i==2){   alert( 'Enter Another Time  for alarm 3 you have another alarm at this time ');   }

                         exist = true;
             break;
                }

                    else { exist = false;   }

    }
   }
       else if (exist == false  && $(this).attr('value') != currentTime  )
    {
           $('.Time_label').each(function(j){  if(j==i){ $(this).removeClass('missed');}    });
        NotifTime = $(this).attr('value');

         TimeArr[j] = NotifTime;
       j= j+1 ;
       count ++;
     }  

      });

     // save data in the DB
 });






 function getExistAlarmsTime(tx)
 {
     tx.executeSql("SELECT Time  FROM AlarmTBL ",[],AlarmTimeExist,transError);


 }

 function AlarmTimeExist(tx,result)
 {

    for(var j=0;j< result.rows.length; j++)
{
        var row = result.rows.item(j);

    ExistAlarmTimesArr[j] = row['Time'];

     }

  }
var报警\u timenum;
var exist=true;
var ExistAlarmTimesArr=[];
$('#AlarmTimes')。在('keyup',function()上{
alarm#TimesNum=$('#AlarmTimes').attr('value');
var timeFields=$(“#timeFields”);
if(timeFields.children().length!=0)
{   
timeFields.empty();
timeFields.hide();
} 

对于(var i=1;i我认为问题可能只是使用
.attr('value')
,请尝试将其分为
.val()
相反。我想这会解决您的问题。

谢谢您的帮助,我已经尝试过了,但问题仍然存在,当我再次单击“保存”按钮时,代码注意到exist变量值从true变为false。请您能帮我更多忙吗?很抱歉,我不能做更多,有很多问题。嗯,标签代替了标签*包装js这取决于document.ready或(function(){}())中的dom;*对attr而不是val的大量引用*使用前未实例化变量db*点击处理程序确实应该调用preventDefault*变量currentTime从未实例化*使用
警报('输入警报时间'+i)
*单击时,处理程序应调用函数,应用SRP更多。*使用
new Date()
和字符串拆分输入以执行您的!=currentTime。这或使用插件会导致您永远无法信任用户以特定格式输入。