jQuery长度验证失败
jQuery length failed to validate
我有这个HTML:
<div id="product_imgfiles_container" class="row">
<div class="col-xs-4 col-md-2">
<div class="thumbnail product_imgfiles">
<img src="./user/product/2016/04/07/2608/0024454001460020491/img/29310931.jpg ">
<a href=""><span><i class="fa fa-times-circle"></i> delete</span></a>
</div>
</div>
<div class="col-xs-4 col-md-2">
<div class="thumbnail product_imgfiles">
<img src="./user/product/2016/04/07/2608/0024454001460020491/img/29340931.jpg ">
<a href=""><span><i class="fa fa-times-circle"></i> delete</span></a>
</div>
</div>
<div class="col-xs-4 col-md-2">
<div class="thumbnail product_imgfiles">
<img src="./user/product/2016/04/07/2608/0024454001460020491/img/29311931.jpg ">
<a href=""><span><i class="fa fa-times-circle"></i> delete</span></a>
</div>
</div>
</div>
我有一个jquery:
$.getJSON("controller/ctrl.dropzonejs-imgfiles.php?dir=XXX",function(json){
$.each( json, function( key, val ) {
alert (val);
if (!$("#product_imgfiles_container img[src="+val+"]").length){
alert ("this val not in your HTML yet");
}
});
});
当没有IF语句时,alert (val);
可以显示所有json数据。但当我需要使用.length
进行验证时,它会停止迭代。并且我仍然不能执行CCD_ 3。
如何正确使用.length
来验证json的src路径是否已经存在于HTML中?
您可能从jQuery/浏览器的选择器引擎中得到一个错误。您需要将从val
获得的值加上引号:
$("#product_imgfiles_container img[src='"+val+"']")
// Note -------------------------------^-------^
您可以使用web控制台查看错误。
将您的代码更改为以下代码:-
if ($("#product_imgfiles_container img[src='"+val+"']").length==0){
alert ("this val not in your HTML yet");
}
我确信从JSON中获取的"val"值无效,"val"具有包括图像名称在内的完整路径。
所以最好只使用图像名称,而不是完整的路径。
img src="/dummy/image/logo_header.png"
var x = $("img[src$='logo_header.png']");
您可以将if条件更改为
if (!$("#product_imgfiles_container img").prop('src') == val){
alert ("this val not in your HTML yet");
}
相关文章:
- 添加@javascript标记时,Cucumber中的HTTP身份验证失败
- postmates api的AJAX中的HTTP身份验证失败
- Spring Security中的基本身份验证(身份验证失败消息)
- 同一页面上的多个验证码在提交失败时消失
- 表单验证失败,但仍提交
- ExtJs 4 GridPanel with CellEdit:输入的数据在验证失败时丢失
- 验证失败时禁用按钮
- 即使验证失败,表单也会被提交
- jQuery验证另一个值失败
- 即使验证失败,表单也会提交
- jQuery长度验证失败
- 当表单验证失败时,我将如何最好地处理下一个事件
- 验证失败时阻止表单提交(Angular Material)
- OAuth错误:签名验证失败
- 如果在ASP.Net MVC视图中验证失败,请设置httppostedfilebase
- reCaptcha-验证失败
- 循环遍历所有字段,如果任何一个字段的验证失败jquery,则返回false
- 全局变量设置在JavaScript验证中失败
- 验证用户名和密码失败-客户端会话
- jQuery 在多步骤表单上验证失败后中断