Jquery 变量为 null 或未定义,但传递验证,例如不为 null 或未定义
Jquery variable is null or undefined, but pass in validation like be not null or not 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");
}
}
}
}
});
}
这有效,但我有一个大问题:验证不起作用!因此,当没有数据或 null 值时,代码将忽略验证并运行 window.open(link,"_blank");
>typeof something
永远不会未定义,它会返回一个字符串。你应该写
if(typeof data != 'undefined')
....
因为未定义类型是"未定义"。
但是,这将允许空值通过。检查不为 null 和未定义的正确方法是:
if(data != null)
...
根据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!!!
}
请将您的验证替换为"未定义"(表示为空)
var x = undefined;
if (typeof x != "undefined")
{
alert(1); // no alert here :)
}
相关文章:
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 为什么localStorage[“..”]未定义,而localStorage.getItem(“..”)为null
- 当null应该在javascript中使用而不是未定义时
- React.createElement:类型不应为null或未定义
- 对象为null或IE9中存在未定义错误
- 如何在JavaScript中检查变量是否为null和/或未定义
- Microsoft JScript运行时错误:无法获取属性'的值;样式':对象为null或未定义
- 无法获取属性'字符串'的未定义或null引用IE 11 ASP.NET
- SCRIPT5007:无法获取属性'长度'的未定义引用或null引用
- 无法获取属性'setActionableButtonState'的未定义引用或null引用
- Highcharts无法设置属性'的值;要点':对象为null或未定义
- IE错误:无法设置属性'的值;样式':对象为null或未定义
- moment:Array.prototype.some在null或未定义时被调用
- 无法获取属性'偏移'的未定义引用或null引用
- 具有null或未定义值的JavaScript字符串串联行为
- Javascript-函数获取数组元素参数未定义/为null
- 无法在jasmine it函数中将未定义或null转换为对象
- JS:未定义值和未定义类型,以及null值和null类型之间的区别
- IE:无法获取属性'的值;clientWidth':对象为null或未定义
- 比较未定义/null/NaN与ActionScript中的number