简体   繁体   English

如何在jQuery中使用变量ID作为选择器

[英]How to use variable id as selector in jQuery

I found a fiddle for a selectbox here: http://jsfiddle.net/davidThomas/5Eebm/ that works nicely but i need to have a dynamic # of selectboxes which means a dynamic # of ids. 我在这里找到了一个选择框的小提琴: http : //jsfiddle.net/davidThomas/5Eebm/效果很好,但是我需要有一个动态的选择框,这意味着一个动态的ID。 Being brand new to jQuery and javascript, how would I go about creating a variable selector in the jq if, for instance I were to use a dynamic [idx] value in my php to increment each id by 1 (eg: prophlist_1, choose_1; prophlist_2, choose_2; etc). 作为jQuery和javascript的新手,如果我要在php中使用动态[idx]值将每个id加1(例如,prophlist_1,choice_1; prophlist_2,choice_2;等)。 Any insight at all would be very appreciated. 任何见解将不胜感激。

here's the fiddle code: 这是小提琴代码:

<ul id="prophlist" style="display:none;">
<li>Prophecy 1 text</li>
<li>Prophecy 2 text</li>
<li>Prophecy 3 text</li>
<li>Prophecy 4 text</li>
<li>Prophecy 5 text</li>
<li>Prophecy 6 text</li>
<li>Prophecy 7 text</li>
<li>Prophecy 8 text</li>
<li>Prophecy 9 text</li>
<li>Prophecy 10 text</li>
</ul>
<select id="choose">
<option value="1">Title 1</option>
<option value="2">Title 2</option>
<option value="3">Title 3</option>
<option value="4">Title 4</option>
<option value="5">Title 5</option>
<option value="6">Title 6</option>
<option value="7">Title 7</option>
<option value="8">Title 8</option>
<option value="9">Title 9</option>
<option value="10">Title 10</option>
</select>

<div id="update">
</div>

$('#choose').change(
function(){
    var i = this.selectedIndex;
    $('#update').text($('#prophlist li').eq(i).text());
});

I need to have a dynamic # of select boxes which means a dynamic # of ids 我需要有一个动态的选择框数,这意味着一个动态的ID数

No. You don't need to use element ids at all to get this functionality working. 不需要。您完全不需要使用元素ID来使此功能起作用。 You can instead give the select elements the same class : 您可以给select元素相同的

<select class="choose">

And then use that class to bind the same change handler to all of them: 然后使用该类将相同的更改处理程序绑定到所有这些更改处理程序:

$(".choose").change(function() { ... });

And then within the handler rather than using an id to find the related div, use DOM navigation methods like .next() to find the div based on its relative position in the markup: 然后在处理程序中,而不是使用id来找到相关的div,而是使用.next()类的DOM导航方法根据div在标记中的相对位置来查找div:

    $(this).next().text(...)

Also, if the display:none; 另外,如果display:none; ul element is there solely to hold the descriptions, why don't you get rid of it and add the descriptions directly to each option like this: ul元素仅用于保存描述,为什么不删除它并将描述直接添加到每个选项中,如下所示:

<option value="1" data-desc="Prophecy text 1">Title 1</option>

Putting that together your function might look like this: 将您的函数放在一起可能看起来像这样:

$('.choose').change(function(){
    $(this).next().text( $(this).find(':selected').attr('data-desc') );
})

Here's an updated version of your fiddle that shows that one little bit of JS handling multiple select elements: http://jsfiddle.net/5Eebm/75/ 这是您的提琴的更新版本,显示了一点点JS处理多个选择元素: http : //jsfiddle.net/5Eebm/75/

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

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