简体   繁体   English

jQuery在动态创建的div中选择动态类ID

[英]jquery selecting dynamic class id inside dynamically created div

I have some JS that dynamically creates a new div (inputs inside of a form). 我有一些JS,可以动态创建一个新的div(表单内的输入)。 It works great. 效果很好。 I separately have some jquery that checks a dropdown input, and shows a separate div if a specific selection is clicked. 我另外有一些jquery检查下拉输入,如果单击特定选择,则会显示一个单独的div。 It works great. 效果很好。

If I try to use the jquery to show a separate div inside one of the dynamically created ones, it does not work. 如果我尝试使用jquery在动态创建的其中一个中显示一个单独的div,它将无法正常工作。 The first instance of it does work, but none of the dynamically created ones. 它的第一个实例确实起作用,但是没有一个动态创建的实例。 After searching around, it looks like I need a delegation, but I can't seem to figure it out for my specific situation. 在四处搜寻之后,似乎我需要一个代表团,但对于我的具体情况,我似乎无法弄清楚。

The JSFiddle: http://jsfiddle.net/Xyzeg/ (change the "CPI" dropdown in the first field, for the event to go off). JSFiddle: http : //jsfiddle.net/Xyzeg/ (更改第一个字段中的“ CPI”下拉列表,以便事件结束)。 The fiddle includes the other JS for dynamically creating the divs. 小提琴包含用于动态创建div的其他JS。

Any pointers would be appreciated. 任何指针将不胜感激。

<div style="float:left">
<div id='dynamicInput'>Pension 1: Amount: $<input type='text' name='adjustmentInputs[]' value='0' size='13' maxlength='20'>Start Year:
<input type='text' name='adjustmentInputs[]' value='2032' size='5' maxlength='6'>
<input type='hidden' name='adjustmentInputs[]' value='2100'>
<select name='adjustmentInputs[]'>
  <option value='InflationAdjusted'>Inflation Adjusted</option>
  <option value='NotInflationAdjusted'>Not Inflation Adjusted</option>
</select>by
<select name='adjustmentInputs[]' class='pensionAdjustment1'>
  <option value='CPI'>CPI</option>
  <option value='constantPercentage'>Constant %</option>
</select>
<div id='constantPercentageText1' style='display: none'>@ <input type='text' name='adjustmentInputs[]' value='3' size='5' maxlength='6'>%</div>
</div>
   <input type="button" value="Add another Portfolio Adjustment" onClick="addInput('dynamicInput');">
</div>
<script>
$("[class*=pensionAdjustment]").change(function() {
    var n = $(this).attr('class').replace('pensionAdjustment', '');
    var newId = "constantPercentageText" + n;
    var selectedItem = $(this).children('option:selected');
    if ($(this).val() == 'constantPercentage') {
        $("#constantPercentageText" + n).css('display', 'inline');
        $("#constantPercentageText" + n).show();
    } else {
        $("#constantPercentageText" + n).css('display', 'none');
    }
});  
</script>

You need to delegate with newly created elements! 您需要委托新创建的元素!

Change this line 更改此行

$("[class*=pensionAdjustment]").change(function() {

to

$(document).on('change',"[class*=pensionAdjustment]",  function() {

You can use 您可以使用

$(document).on('change', '[class*=pensionAdjustment]', function(){
    // your code
});

It would be better if you use a parent div, instead of document . 如果使用父div而不是document会更好。

Update : The elements you are injection dynamically to the DOM were not present when the event was registered, so new elements are not firing the event. 更新:注册事件时,没有动态注入到DOM元素,因此不会触发新元素。 Using event delegation , (registering the event on a parent element) this could be solved because when any element fires an event, the event bubbles up to the last element (upwards) it can find and this way the parent element is being triggered but behind the scene, something like following is happening 使用event delegation (在父元素上注册event )可以解决此问题,因为当任何元素触发事件时,事件会冒泡直到它可以找到的最后一个元素(向上),这样就触发了父元素,但后面现场,正在发生以下类似情况

e = e || window.event;
var el = e.target || e.srcElement;

You may check this . 您可以检查一下

You have to bind the event to a parent element, then filter down to the one you want. 您必须将事件绑定到父元素,然后过滤到所需的元素。 This way it doesn't matter when the element is added, it will always work. 这样,添加元素并不重要,它将始终有效。 This is called delegation. 这称为委派。 You delegate the task of handling the event to another element. 您将处理事件的任务委托给另一个元素。

$(document).on('change', "[class*=pensionAdjustment]", function(){
    var n = $(this).attr('class').replace('pensionAdjustment', '');
    var newId = "constantPercentageText" + n;
    var selectedItem = $(this).children('option:selected');
    if ($(this).val() == 'constantPercentage') {
        $("#constantPercentageText" + n).css('display', 'inline');
        $("#constantPercentageText" + n).show();
    } else {
        $("#constantPercentageText" + n).css('display', 'none');
    }
});

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

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