简体   繁体   English

使用jQuery更改多个元素

[英]Change multiple elements with jQuery

I'm currently trying to change multiple element's values. 我目前正在尝试更改多个元素的值。

However, my script only work using the element ID, and I can't have more than one ID. 但是,我的脚本仅使用元素ID起作用,并且我不能使用多个ID。 What is the alternative, since I don't know how to use getElementsByClassName.. 有什么选择,因为我不知道如何使用getElementsByClassName ..

HTML: HTML:

<select name="valores" id="valores" class="valores" onchange="trocaValores(this.value)">
<option value="v01" selected="selected">Mensal</option>
<option value="v02">Trimestral 5% de desconto</option>
<option value="v03">Semestral 10% de desconto</option>
<option value="v04">Anual 15% de desconto</option>
</select>

jScript: jScript:

function trocaValores(obj) {

combo2=document.getElementById('valores');

for (x=0;x<combo2.length;x++){
    linha=combo2.options[x].value;
    document.getElementById(linha).style.display = "none";
}
//document.getElementById("000").style.display = "none";
//document.pesquisa.cidades.value
itemcombo=combo2.options[combo2.selectedIndex].value;
obj2=document.getElementById(itemcombo);
if (obj2){
    obj2.style.display = '';
}

};

And this is what I need to change: 这就是我需要更改的内容:

<span class="moeda soft-strong" id="v01"><sup><h4>R$ 16.90</h4> </sup> mensais </span>
<span class="moeda soft-strong" id="v02" style="display:none"><sup><h4>R$ 16.90</h4> </sup> trimestral </span>
<span class="moeda soft-strong" id="v03" style="display:none"><sup><h4>R$ 16.90</h4> </sup> semestral </span>
<span class="moeda soft-strong" id="v04" style="display:none"><sup><h4>R$ 16.90</h4> </sup> anual </span>

PS: I have three blocks like that, so I have three v01, v02.. PS:我有3个这样的方块,所以我有3个v01,v02 ..

Thanks! 谢谢!

@EDIT: @编辑:

Example: 例:

Product ONE: 产品一:

<span class="moeda soft-strong" id="v01"><sup><h4>R$ 16.90</h4> </sup> mensais </span>
<span class="moeda soft-strong" id="v02" style="display:none"><sup><h4>R$ 16.90</h4> </sup> trimestral </span>
<span class="moeda soft-strong" id="v03" style="display:none"><sup><h4>R$ 16.90</h4> </sup> semestral </span>
<span class="moeda soft-strong" id="v04" style="display:none"><sup><h4>R$ 16.90</h4> </sup> anual </span>

Product TWO: 产品二:

<span class="moeda soft-strong" id="v01"><sup><h4>R$ 16.90</h4> </sup> mensais </span>
<span class="moeda soft-strong" id="v02" style="display:none"><sup><h4>R$ 16.90</h4> </sup> trimestral </span>

[...] [...]

When changing pricing to product ONE, I need that the monthly price (translation: mensais) of the product TWO change, when changing the quaterly (translation: trimenstral), change of the two products.... and so on! 当更改产品ONE的价格时,我需要更改产品两个月的价格(翻译:mensais),当季度更改(翻译:三边)时,需要更改两个产品...等等。

getElementsByClassName is like getElementById . getElementsByClassName类似于getElementById

 var x=document.getElementById("the_id") //getElementById method

 var y=document.getElementsByClassName("the_class") //getElementsByClassName method

You type the class name in just like you'd type the id in. 您可以像输入ID一样输入类名。

---Edit:--- - -编辑: - -

document.getElementsByClassName returns an array-like object. document.getElementsByClassName返回类似数组的对象。

So do something like this: 所以做这样的事情:

 var first=document.getElementsByClassName("the_class");

 document.write(first[0]);

*Use document.write for testing purposes only. *仅将document.write用于测试目的。


Now if you wanted the price to change at the beginning of your script make a variable: var price="R$ 16.90"; 现在,如果您希望在脚本的开头更改价格,请创建一个变量: var price="R$ 16.90";

Then to change the price do: price*0.95 //5% discount price*0.90 //10% discount price*0.85 //15% discount 然后更改价格,请执行以下操作: price*0.95 //5% discount price*0.90 //10% discount price*0.85 //15% discount

Are you using jQuery or pure javascript? 您使用的是jQuery还是纯JavaScript? Because if you are using jQuery you could use jQuery selectors to search the DOM and find the elements. 因为如果您使用的是jQuery,则可以使用jQuery选择器搜索DOM并查找元素。

$('#valores').change(function() {
    $('span[class^="moeda"]').hide();

    //$(this) would be the combo box and .val() will 
    //give you the value of the selected item
    $('#' + $(this).val()).show();
});

This is all you'd need to do if you used jQuery. 这是使用jQuery所需要做的全部工作。

http://jsfiddle.net/dklingman/S8X6k/ http://jsfiddle.net/dklingman/S8X6k/

@Update @更新

$('#valores').change(function () {
    //Get and hold onto all the spans.  
    //This helps with not needing to search 
    //the DOM each time, one and done
    var spans = $('div[id^="product"] span');

    $(spans).hide();

    //$(this) would be the combo box and .val() will 
    //give you the value of the selected item.
    //Now all we need to do is search, (filter), the
    //set of stored spans for the ones that have an
    //id that starts with the selected value from the
    //combo box.
    $(spans).filter('[id^="' + $(this).val() + '"]').show();
});

http://jsfiddle.net/dklingman/UBEm2/4/ http://jsfiddle.net/dklingman/UBEm2/4/

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

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