简体   繁体   English

生成动态 id 到 javascript

[英]Generating dynamic id into javascript

I'm trying to change the button value from "Add To Cart" to "Loading..." on a page with multiple products listed.我正在尝试在列出多个产品的页面上将按钮值从“添加到购物车”更改为“正在加载...”。 The following code is the furthest I've gotten so far.以下代码是迄今为止我得到的最远的代码。 The only problem is that it changes all button values and not the certain one that's being clicked on the page.唯一的问题是它会更改所有按钮的值,而不是页面上单击的某个值。

<span class="add-to-cart-buttons"> <input type="button" id="AddToCartButton_41099_27479" name="AddToCartButton_41099_27479" class="button call-to-action add-to-cart" value="Add to Cart">  </span>

JS: JS:

<script type="text/javascript">
                $('.button').click(function(){
                $(".button").prop("value", "Loading...");
                    });
</script>

The button ID's are dynamic and generated by the CMS (aspdnSF).按钮 ID 是动态的,由 CMS (aspdnSF) 生成。 This is how the code looks like in the XML file:这是 XML 文件中代码的样子:

<div class="buy-button-beta">
                <xsl:value-of select="aspdnsf:AddtoCartForm(ProductID, VariantID, 1)" disable-output-escaping="yes"/>
                
            
            
            <script type="text/javascript">
                $('.button').click(function(){
                $(".button").prop("value", "Loading...");
                    });
                </script>

I'm not so familiar with xsl elements so is there a way to get the ProductID and VariantID, which I assume is creating the id "AddToCartButton_41099_27479" in this particular item on the page?我对 xsl 元素不太熟悉,所以有没有办法获取 ProductID 和 VariantID,我假设它是在页面上的这个特定项目中创建 id“AddToCartButton_41099_27479”?

You want, I think, to change the button that was clicked on and only that button?你想,我想,改变被点击的按钮,只有那个按钮? You need to use this , rather than the class selector.您需要使用this ,而不是 class 选择器。 The class selector is saying "get all the elements that match this class on this page", which is what is happening. class 选择器说“在此页面上获取与此 class 匹配的所有元素”,这就是正在发生的事情。

So, using your code, it looks like this:因此,使用您的代码,它看起来像这样:

$(this).prop("value", "Loading...");

The better way to do it is not to use jQuery at all for this bit, but plain old Javascript:更好的方法是完全不使用 jQuery,而是使用普通的旧 Javascript:

this.value = "Loading..."; 

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

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