gpt4 book ai didi

javascript - 从jquery中动态生成的下拉按钮获取值

转载 作者:行者123 更新时间:2023-11-30 14:35:34 25 4
gpt4 key购买 nike

我是一个简单的类别表单,由一个按钮 Choose the Item 组成,它的作用类似于下拉组件。最初,内容表单是静态 html,当用户点击 Add Content 时,会动态添加/复制另一个内容表单。

有一个名为Choose the Item, 的按钮,单击该按钮后,会出现一个下拉列表,显示值,用户选择一个值,按钮名称会更改为所选的值。

在此示例中,由于缺少 Bootstrap 库,外观和感觉不一样。

我遇到的问题是,点击事件不适用于第二个动态生成的内容。也就是说,当我添加其他内容时,我无法单击按钮 Choose the Item。有人可以帮我解决这个问题吗?

var count = 1;

$('.js-add').click(function() {
count++;
var content = `

<div class="form-control-group">
<h3>Content No. `+count+`</h3>

<div class="form-control">
<label for="volume">Item</label>

<button type="button" id="volume-menu`+count+`" class="mdl-button js-custom-select mdl-js-button mdl-button--raised mdl-js-ripple-effect">
Choose the item
</button>

<ul class="mdl-menu mdl-js-menu" for="volume-menu`+count+`">
<li class="mdl-menu__item" data-value="10">10</li>
<li class="mdl-menu__item" data-value="20">20</li>
<li class="mdl-menu__item" data-value="30">30</li>
</ul>

</div>
</div>


<br>`

$('.main').append(content);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>

<title>Page Title</title>
</head>
<body>

<div class="btn btn-success js-add">
Add Content
</div>

<div class="main">


<div class="form-control-group">
<h3>Content No. 1</h3>

<div class="form-control">
<label for="volume">Item</label>

<button type="button" id="volume-menu" class="mdl-button js-custom-select mdl-js-button mdl-button--raised mdl-js-ripple-effect">
Choose the item
</button>

<ul class="mdl-menu mdl-js-menu" for="volume-menu">
<li class="mdl-menu__item" data-value="10">10</li>
<li class="mdl-menu__item" data-value="20">20</li>
<li class="mdl-menu__item" data-value="30">30</li>
</ul>
</div>
</div>


<br>



</div>



</body>

最佳答案

There is a button called Choose User that when clicked, it gives a drop down list, showing the values and the user selects a value, the button name changes to that value selected.

添加这部分代码

$(document.body).on("click", "li.mdl-menu__item", function() {
$(this).parent().prev().text($(this).text());
$(this).parent().toggleClass('hidden');
});

the click event is not working on the second generated dynamically content. That, is I am unable to click the button Choose User when I add another category.

这种类型的事件定义解决了这个问题并且您使用了它。您的代码运行良好,我没有遇到这个问题。

$(document.body).on("click", "button.js-user-select", function() {
$(this).next().toggleClass('hidden');
});

我认为这对你有帮助

$(document.body).on("click", "button.js-user-select", function() {
$(this).next().toggleClass('hidden');
});

$(document.body).on("click", "li.mdl-menu__item", function() {
$(this).parent().prev().text($(this).text());
$(this).parent().toggleClass('hidden');
});

var count = 1;

$(".add-other").click(function() {
count++;
var content =
`<div class="form-control-grp">
<h3>Category ` +
count +
`</h3>

<div class="form-control">
<label for="volume">Type of User</label>

<button type="button" id="user-list" class="mdl-button js-user-select mdl-js-button mdl-button--raised mdl-js-ripple-effect">
Choose user
</button>

<ul class="mdl-menu mdl-js-user hidden" for="user-list">
<li class="mdl-menu__item" data-value="10">User Specialist</li>
<li class="mdl-menu__item" data-value="20">User Moderator</li>

</ul>

<input type="hidden" id="user" name="user">
</div>
</div>`;

$(".main").append(content);
});
.hidden {
display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="submit" class="add-other">Add Category </button>

<div class="main">

<div class="form-control-grp">
<h3>Category 1</h3>

<div class="form-control">
<label for="volume">Type of User</label>

<button type="button" id="user-list" class="mdl-button js-user-select mdl-js-button mdl-button--raised mdl-js-ripple-effect">
Choose user
</button>

<ul class="mdl-menu mdl-js-user hidden" for="user-list">
<li class="mdl-menu__item" data-value="10">User Specialist</li>
<li class="mdl-menu__item" data-value="20">User Moderator</li>

</ul>

<input type="hidden" id="user" name="user">
</div>
</div>


更新

1- 添加这个用于更新dom

componentHandler.upgradeDom();

2- 为按钮文本添加这个

$(document).on("click", "li.mdl-menu__item", function() {
$(this).parent().parent().prev().text($(this).text());
});

3- 使用 ${count} 而不是您的用法使 id 动态化。

var count = 1;

$(document).on("click", "li.mdl-menu__item", function() {
$(this).parent().parent().prev().text($(this).text());
});

$('.js-add').click(function() {
count++;
var content = `
<div class="form-control-group">
<h3>Content No. ${count}</h3>

<div class="form-control">
<label for="volume">Item</label>

<button type="button" id="volume-menu-${count}" class="mdl-button js-custom-select mdl-js-button mdl-button--raised mdl-js-ripple-effect">
Choose the item
</button>

<ul class="mdl-menu mdl-js-menu" for="volume-menu-${count}">
<li class="mdl-menu__item" data-value="10">10</li>
<li class="mdl-menu__item" data-value="20">20</li>
<li class="mdl-menu__item" data-value="30">30</li>
</ul>

</div>
</div>


<br>`

$('.main').append(content);
componentHandler.upgradeDom();

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>

<title>Page Title</title>
</head>

<body>

<div class="btn btn-success js-add">
Add Content
</div>

<div class="main">

<div class="form-control-group">
<h3>Content No. 1</h3>

<div class="form-control">
<label for="volume">Item</label>

<button type="button" id="volume-menu-1" class="mdl-button js-custom-select mdl-js-button mdl-button--raised mdl-js-ripple-effect">
Choose the item
</button>

<ul class="mdl-menu mdl-js-menu" for="volume-menu-1">
<li class="mdl-menu__item" data-value="10">10</li>
<li class="mdl-menu__item" data-value="20">20</li>
<li class="mdl-menu__item" data-value="30">30</li>
</ul>
</div>
</div>

<br>

</div>

</body>

关于javascript - 从jquery中动态生成的下拉按钮获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50480678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com