Jquery变量为null或未定义,但传入验证,如非空或未定义

时间:2014-11-27 17:03:32

标签: javascript jquery validation null undefined

我有这段代码,重复“n”次......

          <div class="col-sm-6">
        <div class="well">
          <fieldset>
              <Legend>Pronto</legend>
              <?php
              $where = '1000,1009';
              $parse=sqlquery($conn,$where);
              while(OCIFetchInto($parse,$campo,OCI_ASSOC)){
                  corpanel($campo['SCORE']);
                  $num_tarefa = str_replace($trocarIsso,$porIsso,$campo['NUM_DEMANDA']);
                  ?>
                  <div class="panel-heading">
                      <h3 class="panel-title">
                          <ul class="nav nav-pills">
                              <li>
                                  <a href="#">Tarefa: <?php echo $num_tarefa; ?></a>
                              </li>
                              <?php Alterastatus($num_tarefa);?>
                          </ul>
                      </h3>
                  </div>
                  <div class="panel-body" >
                      <center>
                          <div id="box-toggle" name="box-toggle">
                            <div class="tgl"> 
                              <?php echo $titulo.'<br/>';?>
                              <label class="control-label" >Analista: </label><?php echo $analista.'<br/>'; ?>
                              <label class="control-label" >Programador: </label><?php echo $programador.''; ?><br/>
                              <label class="control-label" >Data Prevista:</label>
                              <input name="calendario" class="form-control input-sm" type="text" value="<?php echo $data;?>">
                              <label class="control-label" >Centro de Custo:</label>
                              <?php printf("<br/>.$centroCusto.<br/>"); ?>
                            </div>
                          </div> 
                      </center>
                  </div>
                </div>

              <?php } ?>
          </fieldset>
        </div>
      </div>    

要获取输入的值,我只能使用最接近的并找到。所以我写了这个jquery代码

  <script>
function Alterardata(urlatualizar){
  $( document ).on( "click", function( event ) {
     var numdemanda = null;
     var data = null;
     numdemanda = $(event.target).closest(".inpdem").find("input.valordata").val();
     data = $(event.target).closest(".inpdem").find("input#data").val();
     var link = "/controle_versoes/controle_tarefas/update.php?demanda="+numdemanda+"&dataprev="+data;
     if (typeof numdemanda !== null && !(numdemanda.match(/^\s+$/))){
      if (typeof numdemanda !== undefined){
        window.alert(numdemanda);  
        if(typeof data != null && !(data.match(/^\s+$/))){
          window.alert('não é nula '+data);
          if (typeof data != undefined ){
            window.alert('indefinida' + data) ;  
            window.open(link,"_blank");       
          }
        }
      }  
    } 
  });
}

这有效,但我有一个大问题:验证不起作用!因此,当没有数据或空值时,代码会忽略验证并运行window.open(link,“_ blank”);

2 个答案:

答案 0 :(得分:0)

typeof something永远不会被定义,它会返回一个字符串。你应该写

if(typeof data != 'undefined')
....

因为未定义的类型未定义&#39;。

但是,这会让null值通过。检查非空且未定义的正确方法是:

if(data != null)
...

答案 1 :(得分:0)

根据mozilla docs(在此处找到:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

typeof永远不会返回null ..因此实际返回字符串所以:

var x = undefined;

if (typeof x != null)
{
    alert(1); // the alert is poping up!!!
}

请将您的验证替换为“undefined”(无效)

var x = undefined;

if (typeof x != "undefined")
{
    alert(1); // no alert here :)
}