简体   繁体   English

如何使用jQuery检查具有自定义属性和特定值的输入字段

[英]How can i check input field that has custom attribute with specific value using jQuery

I have input fields with custom attributes. 我有带有自定义属性的输入字段。 i want to check if input field with custom attribute has specific value then alert match else not match. 我想检查具有自定义属性的输入字段是否具有特定值,然后警报匹配,否则不匹配。

(function ($) {
    $(".my_class").click(function () {
        var custom_attr = $(this).attr('custom_attr');
        $("input").each(function () {
            var condation = $(this).attr('condation');
            if (condation == custom_attr) {
                alert('match');
            } else {
                alert('not match');
            }
        });
    });
})(jQuery);

JS Fiddle Link JS小提琴链接

I think you can try a regex bases test like 我认为您可以尝试像这样的正则表达式基础测试

 if (!RegExp.escape) { RegExp.escape = function(value) { return value.replace(/[\\-\\[\\]{}()*+?.,\\\\\\^$|#\\s]/g, "\\\\$&") }; } (function($) { $(".my_class").click(function() { var custom_attr = $(this).attr('custom_attr'); var regex = new RegExp(custom_attr.split(/\\s+/).map(function(value) { return '(\\\\b' + RegExp.escape(value) + '\\\\b)' }).join('|')); $('input').each(function() { var condation = $(this).attr('condation'); if (regex.test(condation)) { alert('match'); } else { alert('not match'); } }); }); })(jQuery); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div> <button type="button" name="abc" class="my_class" custom_attr="field1 field2">Match</button> </div> <div> <input name="def" condation="field1 user_defined" /> <input name="jkl" condation="field2 user_defined" /> <input name="jkl" condation="field13 user_defined" /> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用jQuery选择具有特定属性唯一值的每个复选框的第一次出现? - How can I select the first occurence of each checkbox that has a unique value for a specific attribute using jQuery? 如何使用 Jquery 设置输入字段的 Name 属性? - How can I set the Name attribute of an input field using Jquery? 使用JQuery获取具有特定值属性的输入值 - Get Input value that has attribute with specific value using JQuery 检查输入文本字段是否具有特定数值并使用 jquery 更改选择的选定选项 - Check if an input text field has a specific numeric value and change the selected option of a select using jquery 如何获取使用jQuery用户输入到输入字段中的值 - How can I get the value that a user has entered into an input field using jQuery 如何在jQuery中通过自定义属性获取输入字段的值 - How to get the value of input field by custom attribute in jquery 如何使用Jquery设置隐藏输入字段的值? - How can I set the value of a hidden input field using Jquery? 使用jQuery更改输入字段上的自定义属性? - Changing custom attribute on input field using jQuery? 如何使用jquery选择具有&#39;abbr&#39;属性特定值的元素 - How can I select element that has specific value for 'abbr' attribute with jquery 如何使用Jquery检查子字符串在同一输入中是否具有相同的值 - how to check if substring has a same value in same input using Jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM