【问题标题】:Validate an input field in order to be unique and non duplicate in jQuery mobile验证输入字段以便在 jQuery mobile 中唯一且不重复
【发布时间】:2014-02-15 11:09:52
【问题描述】:

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

else if (exist == false && $(this).attr('value') != currentTime ) 直接“一旦”更新存在值,我需要再次单击保存按钮才能制作代码知道存在值改变了,继续在数据库中保存报警数据。

我该如何解决这个问题?请帮帮我...

<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'];

     }

  }

【问题讨论】:

    标签: validation jquery-mobile unique


    【解决方案1】:

    我认为问题可能只是 .attr('value') 的使用,请尝试将其替换为 .val()。我认为这将解决您的问题。

    【讨论】:

    • 感谢帮助,我已经尝试过了,但问题仍然存在,当我再次单击保存按钮时,代码注意到存在变量值从 true 到 false 的变化
    • 你能帮我更多吗??
    • 对不起,我不能用它做更多,有很多问题。
    • Ummm *label 代替 lable *wrap js 依赖于 document.ready 中的 dom 或 (function() {})(); *lots 引用 attr 而不是 val *variable db 在使用前未实例化 *点击处理程序确实应该调用 preventDefault *变量 currentTime 永远不会实例化 *使用 alert('Enter time for alarm ' + i) *点击处理程序应该调用函数,更多地应用 SRP。 *使用new Date() 和字符串拆分输入来做你的!= currentTime。那或使用插件会导致您永远无法相信用户以特定格式输入。
    猜你喜欢
    • 2020-07-07
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多