我想在提交表单时检查我的<ul>
是否有孩子。
这是我的html:
<form>
/*other input elements*/
<div class="attributes-added">
<ul name = "attributesAddedList">
// by default no element is present
</ul>
<input type = "submit" value = "save"> </div>
</form>
正如您所看到的,默认情况下,提供的ul具有子项(<li>
)。用户需要动态添加它们,这不是问题。
但我需要的是,当我提交表单时,应该使用自定义函数检查ul
中jQuery validator
是否有孩子。
我已经尝试过如下的验证器,但还没有取得丰硕的成果。
$("#templateForm").validate({
rules: {
attributesAddedList:{
addedCategory: $(".attributes-added ul").has("li").length
}
},
message: {
attributesAddedList:{
addedCategory: "the message"
}
}
});
现在进行自定义功能
$.validator.addMethod("addedCategory", function(value, element, len) {
if(len < 0){
return false;
}
}, "choose a category");
任何帮助?
答案 0 :(得分:2)
我使用隐藏的输入来添加规则, 但我建议使用提交处理程序,但这样你就可以使用.valid()方法,而不必等待提交表单, 希望这有帮助
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/jquery.validate.min.js"></script>
<form id="templateForm">
/*other input elements*/
<div class="attributes-added">
<input type="hidden" id="attr" name="attr">
<ul name = "attributesAddedList">
// by default no element is present
</ul>
<input type = "submit" value = "save"> </div>
</form>
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}