这是我的 HTML 代码:
<div class="editable-select-holder">
<input type="text" name="position" id="position" value="${userInfoForm.position}">
<button type="button" class="editable-select-expander"></button>
<ul class="editable-select-options">
<c:forEach var="position" items="${positions}">
<li>${position.description}</li>
</c:forEach>
</ul>
</div>
这是 Jquery:
$(document).on('click', '.editable-select-expander', function () {
var $holder = $(this).parent();
$holder.toggleClass('editable-select-expanded');
$holder.on('click', 'li', function () {
var $t = $(this);
$holder.removeClass('editable-select-expanded');
$holder.find('input').val($t.text());
});
});
我想在单击按钮或专注于输入时扩展列表。我上面提供的代码仅适用于单击按钮。我在焦点事件上也需要相同的逻辑。有什么建议吗?
这样改了,还是不能正常工作。
$(document).on("click",'.editable-select-holder', function() {
var $holder = $(this);
$holder.on('click','.editable-select-expander', function() {
$holder.toggleClass('editable-select-expanded');
});
$holder.on('click', 'li', function() {
var $t = $(this);
$holder.removeClass('editable-select-expanded');
$holder.find('input').val($t.text());
});
$holder.on('focus', 'input', function() {
$holder.addClass('editable-select-expanded');
});
});
最佳答案
希望您能使用此代码
$( your input box id or class ).focusin(function() {
//your logic here
});
关于javascript - 可编辑选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31787739/