![](/img/trans.png)
[英]How can I pass a value from input field to a select option using jquery?
[英]Why can't I select this input field using this regex filter in jQuery?
所以我使用padolsey的正則表達式過濾器來嘗試選擇動態生成的嵌套表單字段。 為什么我在下面的選擇器代碼中繼續為測試獲取空值?
jQuery.expr[':'].regex = function(elem, index, match) {
var matchParams = match[3].split(','),
validLabels = /^(data|css):/,
attr = {
method: matchParams[0].match(validLabels) ?
matchParams[0].split(':')[0] : 'attr',
property: matchParams.shift().replace(validLabels,'')
},
regexFlags = 'ig',
regex = new RegExp(matchParams.join('').replace(/^s+|s+$/g,''), regexFlags);
return regex.test(jQuery(elem)[attr.method](attr.property));
}
//selector code
$(document).ready(function() {
var test = $('input:regex(id, agreement_activities_attributes_\d*_id)');
console.log(test);
});
包含我想要選擇的輸入字段的HTML代碼
<input id="agreement_activities_attributes_0_id" name="agreement[activities_attributes][0][id]" type="hidden" value="28" />
代碼中有錯誤。
.replace(/^s+|s+$/g,'')
應該
.replace(/^\s+|\s+$/g,'')
然后它應該工作。
替換刪除任何周圍的空格(修剪字符串)。 您在選擇器中使用的空間( :regex(id, agreement_activities_attributes_\\d*_id)
)將包含在匹配模式中,因此僅匹配具有以空格開頭的id的元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.