如何在javascript中动态添加字段来匹配id? [英] How to match id in javascript for dynamically added field on html?

查看:84
本文介绍了如何在javascript中动态添加字段来匹配id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的Rails应用程序中,可以将动态字段添加到表单中。以下是动态添加字段的一个html源代码: order_order_items_attributes_1413563163040_unit_price

  < div class =input decimal required order_order_items_unit_price> 
< label class =decimal required control-labelfor =order_order_items_attributes_1413563163040_unit_price>
< abbr title =required> *< / abbr>
单价($)
< / label>
< input id =order_order_items_attributes_1413563163040_unit_priceclass =numeric decimal required span5 span5type =numberstep =anyname =order [order_items_attributes] [1413563163040] [unit_price]>
< / div>

正如您所看到的,字段ID中有13位数字字符串,并且在字段中随机生成被添加。我们如何在JavaScript中匹配(定位)这种类型的随机ID? rails应用程序使用jquery(例如, $('#order_order_items_attributes_xxxxxxxxxxxxx_unit_price')。change(function(){}))。

我们对这种css类型的id匹配很陌生。更多细节将被赞赏。

解决方案

您必须先决定使用哪种算法来匹配id值。根据你的意见(在你的问题中没有详细说明),看起来你想要找到所有以order_order_items_attributes _开头并以__ unit_price并在它们之间有一个数字序列。



你可以这样做,找到所有以你想要的东西开头的id,然后将它们过滤为只符合所有三个标准的东西:

  //找到与此模式匹配的ID:order_order_items_attributes_xxxxxxxxxxxxx_unit_price 
var orderItemRegex = / ^ order_supplier_id_\d + _unit_price $ /
$([id ^ ='order_supplier_id _'])。filter(function(index){
return orderItemRegex.test(this.id);
})。change(function ){
//这将只是与
匹配的id));

这使用jQuery创建一个以<$ c $开头的所有对象列表C> order_supplier_id _。然后它通过该列表进行过滤,消除不匹配定义模式的完整正则表达式 / ^ order_supplier_id_ \ d + _unit_price $ / 的任何对象,然后连接< code。.change()事件处理程序仅用于通过正则表达式测试的对象。


In our rails app, dynamic fields can be added to the form. Here is a html source code for the dynamically added field order_order_items_attributes_1413563163040_unit_price:

<div class="input decimal required order_order_items_unit_price">
<label class="decimal required control-label" for="order_order_items_attributes_1413563163040_unit_price">
<abbr title="required">*</abbr>
Unit Price($)
</label>
<input id="order_order_items_attributes_1413563163040_unit_price" class="numeric decimal required span5 span5" type="number" step="any" name="order[order_items_attributes][1413563163040][unit_price]">
</div>

As you can see, there is 13 digits string in field's id and it is randomly generated when the field is added. How we can match (locate) this type of random id in javascript? rails app uses jquery (ex, $('#order_order_items_attributes_xxxxxxxxxxxxx_unit_price').change(function (){})).

We are new to this css type of id match. More detail would be appreciated.

解决方案

You have to first decide what algorithm you're using for matching the id values. Based on your comments (it is not specified precistly in your question), it appears you want to find all ids that start with "order_order_items_attributes_" and end with "_unit_price" and have a sequence of digits between them.

You can do that like this by find all the ids that start with the thing you want and then filtering them to things that only match all three criteria:

// find ids that match this pattern: order_order_items_attributes_xxxxxxxxxxxxx_unit_price
var orderItemRegex = /^order_supplier_id_\d+_unit_price$/;
$("[id^='order_supplier_id_']").filter(function(index) {
    return orderItemRegex.test(this.id);
}).change(function() {
    // this will be only the ids that match
});

This uses jQuery to make a list of all objects that have an id that starts with "order_supplier_id_". It then filters through that list eliminating any objects who don't match the full regex /^order_supplier_id_\d+_unit_price$/ that defines your pattern and then hooks up the .change() event handler to only the objects that pass the regex test.

这篇关于如何在javascript中动态添加字段来匹配id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆