jquery 验证输入但使用 id 来识别字段

标签 jquery jquery-validate

我有以下格式的字段:

<input type="text" name="cities[]" id="city_zip1"/>
<input type="text" name="cities[]" id="city_zip2"/>
<input type="text" name="cities[]" id="city_zip3"/>

有没有办法使验证插件需要第一个,但保留名称不变? 或者将这三项之一设为必需,但保留名称不变。

最佳答案

例如,点击即可完成。可以在提交时完成。

$("#checkforvalue").click(function(e) {
    e.preventDefault();
    if ($("input#place_of_services:checked").val()) {
       var zipentered = false;
       $("input[id^='city_zip']").each(function() {
           if ($(this).val().length) {
               zipentered = true;
           }
       });

       if (zipentered) {
           alert("value in one of the fields");
       } else {
           alert("no value entered");
       }
   }
});

js fiddle example

关于jquery 验证输入但使用 id 来识别字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7159907/

相关文章:

javascript - 如何使用 jQuery/AJAX 在 HTML div 中加载 html 页面内容?

javascript - $(document).ready 中 Uncaught ReferenceError

javascript - 通过 jQuery Validate 插件使用 HTML 5 验证消息

当复选框更改时 jQuery 重新验证文本字段

jquery - bootstrap 中的下拉菜单 + 而不是列表我想要下拉列表中的 X 列列表

javascript - 如何控制 requestAnimationFrame 的波速

javascript - 创建基本的人体测试验证

jquery 验证不起作用

javascript - 图像 slider 导航按钮

jquery - 使 DIV 中除其他链接之外的所有元素都充当链接