【问题标题】:How do I validate fields with the required attribute or not null?如何验证具有必需属性或不为空的字段?
【发布时间】:2016-04-14 17:16:35
【问题描述】:

我正在做一些验证,但我的if 语句无法正常工作。我想验证具有数据类型和所需属性的字段。或者如果一个字段不为空?

我的 if 语句:

if (typeof $(this).attr("data-type") != "undefined" 
        && typeof $(this).prop("required") != "undefined"
        || typeof $(this).prop("required") === true
        || $(this).val() != "") {
    // Doing stuff
}

HTML 输入:

<input type="text" name="Consignee" id="text" data-type="text"
       placeholder="Consignee" required>

【问题讨论】:

  • 使用$(":input[required]")选择元素
  • 你能在我的 if 语句中告诉我这个吗?我真的很挣扎
  • 你能给我们看完整的例子吗?
  • 真的很抱歉,它太大了 :( 那只是外部 IF,然后所有验证都在内部进行。我使用 $.each 循环遍历所有字段供参考
  • "我想验证具有数据类型和所需属性的字段。或者如果字段不为空?"从字面上看没有任何意义。

标签: javascript jquery validation if-statement


【解决方案1】:

没有要求 typeof。 试试这个查询...

if ($(this).attr("data-type") != "" && $(this).prop("required") != false && $(this).val()!="") {}

【讨论】:

    猜你喜欢
    • 2014-10-08
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2021-07-20
    • 2015-07-19
    • 2019-06-24
    • 2019-01-22
    相关资源
    最近更新 更多