本文介绍了在jQuery循环中获取以某些字符串开头的id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:

我需要获取TD元素中的一些动态ID,将其作为参数传递并调用一个特定的函数。

I need to get some dynamic ids that are inside a TD element, to pass it as a parameter and call an specific function.

我在TD中添加了一个类( .calcStartPrice ),这样它可以帮助我迭代它的元素:

I added a class (.calcStartPrice) to the TD, so that it helps me iterating inside its elements:

    var inputEl, eventStartPrice, exchangeRate, convertedStartPriceEl, currSymbol, decimalPlaces;
jQuery(".calcStartPrice").each(function (i,e) {
    jQuery(e).find('span, input').each(function (a,b) {
        console.info(b.id);
    });
});

当我运行此代码时,我有以下ID:

When I run this code, I have the following ids:

eventStartPrice_S20_L10140
S20_L10140_startPrice
exchangeRate_S20_L10140
curSymbol_S20_L10140
decPlaces_S20_L10140
converted_StartPrice_S20_L10140

现在,我要做的是检查id是否以eventStartPrice开头,以便我将id归于a变量。

Now, what I want to do is to check whether the id starts with eventStartPrice, for example, so that I'll attribute the id to a variable.

我尝试了什么:

var eventStartPrice;
jQuery(".calcStartPrice").each(function (i,e) {
    jQuery(e).find('span, input').each(function (a,b) {
        //console.info(b.id);
        if (jQuery(b[id^="eventStartPrice"])) { //this is wrong!!!
            eventStartPrice = b.id;
            console.info(eventStartPrice);
        }
    });
});

但它不起作用...
如何检查第二次迭代内部如果id以某个字符串开头?

But it didn't work...How can I check inside that second iteration if the id starts with some string?

推荐答案

替换:

if (jQuery(b[id^="eventStartPrice"])) { //this is wrong!!!

使用:

if (/^eventStartPrice/.test(b.id)) {

这篇关于在jQuery循环中获取以某些字符串开头的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:21