[英]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);
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.