简体   繁体   English

如何在名称属性 JQuery 中查找动态 ID

[英]how to find a dynamic id in name attribute JQuery

I have many inputs where to dynamically change the value of the name attribute, for example:我有很多输入可以动态更改名称属性的值,例如:

Select Select

<select class="form__input form__select select optional" id="js-customer-field" name="customer[customer_food_attributes][16321232322354][customer_id]"><option value="">Select</option>

... ...

<select class="form__input form__select select optional" id="js-customer-field" name="customer[customer_food_attributes][023912321331123][customer_id]"><option value="">Select</option>

I would like to take the value to apply to an event, but since the id's are random I don't know how to capture them我想将值应用于事件,但由于 ID 是随机的,我不知道如何捕获它们

on this name attribute or any with random id:在此名称属性或任何具有随机 ID 的属性上:

"customer[customer_food_attributes][023912321331123][customer_id]" “客户[customer_food_attributes][023912321331123][customer_id]”

$("customer[customer_food_attributes][023912321331123][customer_id]").on('change'), function(e, item) {...})

I would be very grateful if someone could help me build the attribute dynamically Thank you for your time in reading me.如果有人可以帮助我动态构建属性,我将不胜感激感谢您花时间阅读我的文章。

It is not really clear to what you are trying to do.您要做什么还不是很清楚。 Could you maybe clarefy a little bit?你能澄清一点吗? Are you trying to change the name attribute of the select elements?您是否尝试更改 select 元素的名称属性? Or do you want to bind an event to all select elements?还是要将事件绑定到所有 select 元素?

If the latter, I would do something like this (assuming you want to trigger the event on a value change):如果是后者,我会做这样的事情(假设您想在值更改时触发事件):

$(".form__select").on("change", function(){
    // Get the name attribute
    let name = $(this).attr("name");
    // Do whatever you like, depending on the value of name
    // ...
};

You could combine "attribute starts with" ( ^= ) and "attribute ends with" ( $= ) selectors to match something with an attribute that looks like:您可以结合“属性以”( ^= )和“属性以”( $= )选择器来匹配具有如下属性的内容:

customer[customer_food_attributes][...][customer_id]

Where ... is any text.其中...是任何文本。

For example:例如:

 $('[name^="customer[customer_food_attributes]["][name$="][customer_id]"]').on('change', function() { console.log(`Select ${$(this).attr('name').match(/\[(\d+)\]/)[1]} changed`) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select class="form__input form__select select optional" id="js-customer-field" name="customer[customer_food_attributes][16321232322354][customer_id]"> <option value="1" selected>1</option> <option value="2">2</option> </select> <select class="form__input form__select select optional" id="js-customer-field" name="customer[customer_food_attributes][023912321331123][customer_id]"> <option value="3">3</option> <option value="4" selected>4</option> </select>

You could just listen to change events from any select elements and check if the name matches the pattern.您可以只监听来自任何 select 元素的更改事件,并检查名称是否与模式匹配。

This can be done with/without jQuery.这可以在有/没有 jQuery 的情况下完成。

 // using jquery
$('select').on('change', function(e) {
    const regex = /customer\[customer_food_attributes\]\[(\d+)\]\[customer_id\]/;
    const match = e.target.name.match(regex);
    if(match){
        const id = match[1];
        // here is your id
        // do something with it
    }
});

// using vanilla js
document.addEventListener('change', function(e) {
    const regex = /customer\[customer_food_attributes\]\[(\d+)\]\[customer_id\]/;
    const match = e.target.name.match(regex);
    if(match){
        const id = match[1];
        // here is your id
        // do something with it
    }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM