简体   繁体   English

选择属性的一部分-JQuery

[英]Select Part of a Attribute - JQuery

I need to get the number only from an attribute (The number is dynamic). 我只需要从属性中获取数字(数字是动态的)。 The button/anchor looks like this: 按钮/锚看起来像这样:

<a href="#" class="btn btn-danger btn-small" data-removefield="collection" data-field="bc_inventorybundle_menu_product_0">Delete Dish</a>

The part I need to dissect is this bit 'bc_inventorybundle_menu_product_0' I only want the number, for use in another function (Delete a LI with an ID of menuitem0_dish) 我需要剖析的部分是此位'bc_inventorybundle_menu_product_0',我只想要该数字,以用于其他功能(删除ID为menuitem0_dish的LI)

The code I use for selecting ID's elsewhere is: 我用于选择其他ID的代码是:

  function getNum(element, attrPrefix) {
      //set prefix, get number
      var prefix = attrPrefix;
      var num = element.attr("id").substring((prefix.length));
      return num;
  }

It works great on ID's but I cant seem to get it to work for Attributes instead of ID's 它在ID上效果很好,但是我似乎无法让它在Attributes而不是ID上起作用

So User clicks delete button bc_inventorybundle_menu_product_0 then jQuery removes the < li id="menuitem0_dish"> 因此,用户单击删除按钮bc_inventorybundle_menu_product_0,然后jQuery删除<li id =“ menuitem0_dish”>

I can't add an ID to the button so I have to use the attribute of the button. 我无法向按钮添加ID,因此必须使用按钮的属性。 As I'm sure you can tell I'm a complete noob when it comes to JS/JQuery. 我敢肯定,当涉及到JS / JQuery时,您可以说我是一个完整的菜鸟。

EDIT 编辑

Having read all the answers I feel I may need to elaborate a little. 阅读完所有答案后,我觉得可能需要详细说明。

I think the biggest issue is registering when the Button/Anchor is clicked. 我认为最大的问题是单击按钮/锚点时注册。

What I currently have is this, which I know must be wrong: 我目前所拥有的是这个,我知道肯定是错误的:

  $(document).on('click', 'data("field")', function(event) {
       deleteDish(this);
  });

  function getbutNum(element, attrPrefix) {
       //set prefix, get number
       var prefix = attrPrefix;
       var butnum = element.data("field").substring(prefix.length); //Changed as per suggestions
       return butnum;
  }

  function deleteDish(field) {
       var numbut = getbutNum();
       //Delete the UL/LI
       console.log("Num But" + numbut);
       }

Asides from all else this gives me an error of 'unrecognized expression: data("field")' 从其他方面来看,这给了我一个'无法识别的表达式:data(“ field”)'的错误。

Have you tried selecting your actual data attribute: 您是否尝试过选择实际数据属性:

var num = element.attr("data-field").substring(prefix.length);

Or: 要么:

var num = element.data("field").substring(prefix.length);

EDIT 编辑

First add a class to your anchor element (I'm going under the assumption that you have more than one of these): 首先,将一个类添加到您的锚元素(假设您有多个此类):

<a href="#" class="btn btn-danger btn-small delete-dish" data-removefield="collection" data-field="bc_inventorybundle_menu_product_0">Delete Dish</a>

Then: 然后:

$(".delete-dish").on("click", function (e) {
    e.preventDefault();

    var fieldData = $(this).data("field"),
        num = fieldData.substring(fieldData.lastIndexOf("_") + 1);

    console.log("Num But" + num);
});

Here is a fiddle to demonstrate 这是一个小提琴来演示

Using the attribute name that contains your input should work: 使用包含您的输入的属性名称应该可以:

function getNum(element, attrPrefix) {
      //set prefix, get number
      var prefix = attrPrefix;
      var num = element.attr("data-field").substring((prefix.length));
      return num;
}

http://jsfiddle.net/zf3hmo4q/ http://jsfiddle.net/zf3hmo4q/

Maybe something like this? 也许是这样的吗?

var getNumberFromAttribute = function(id, field) {
    var field = $(id).data(field);
    var parts = field.split("_");
    return parts[parts.length - 1]
}

Here's a jsfiddle http://jsfiddle.net/o6go79cL/ 这是一个jsfiddle http://jsfiddle.net/o6go79cL/

UPDATE 更新

You could just pass in the element. 您可以只传递元素。 The only purpose of the id was to select the object. id的唯一目的是选择对象。 So you could also just do: 因此,您也可以这样做:

var getNumberFromAttribute = function(elm, field) {
    var field = $(elm).data(field);
    var parts = field.split("_");
    return parts[parts.length - 1]
}

number = getNumberFromAttribute(anchorTag, "field");

Considering you want to parse attributes with "data-*" name: 考虑到您要解析具有“ data- *”名称的属性:

function getNum(element, dataName, dataPrefix) {
    var num = element.data(dataName).replace(dataPrefix, "");
    return num;
}
console.log(getNum($(".btn"), "field", "bc_inventorybundle_menu_product_"));

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

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