[英]Enable Select Option on Button Click
I want to be able to add a div for the select option and disable it within the select dropdown.我希望能够为 select 选项添加一个 div,并在 select 下拉列表中禁用它。
This I have working, with a delete button, however I want to re-enable the select option if the delete button is pressed - this is what I have so far been unable to do.我正在使用删除按钮,但是如果按下删除按钮,我想重新启用 select 选项 - 这是我迄今为止无法做到的。
Can anyone kindly assist?任何人都可以帮忙吗?
jQuery solution preferred but JavaScript solution also good.首选 jQuery 解决方案,但 JavaScript 解决方案也不错。 Open to solutions such as hide/show instead of disable/enable.
打开隐藏/显示而不是禁用/启用等解决方案。
$('select').on('change', function(e) { $('#here').append('<div>' + $(this).val() + '<button>DELETE</button> </div>'); $('option:selected', this).attr('disabled', true); }); $("div").on("click", "button", function(e) { e.preventDefault(); var value = $(this).val(); $('select').find($('option:selected', this)).attr('disabled', false); $(this).parent().remove(); });
div { border: 1px solid #999; padding: 15px }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select> <option>Please select one</option> <option>Volvooooo</option> <option>Saaaaaab</option> <option>Mercedeeeeeeees</option> <option>Audiiiiiii</option> </select> <div id="here">Nothing selected yet</div>
Note that as far as I am aware, this is not a duplicate question - and the question that it has been marked as a duplicate of most certainly does not relate to my question, which is specifically about removing disabled property on button click.请注意,据我所知,这不是一个重复的问题——它被标记为重复的问题肯定与我的问题无关,我的问题特别是关于在单击按钮时删除禁用的属性。 Please do not close my question unless you actually find a proper duplicate (which I couldn't with nearly two hours of searching SO).
请不要关闭我的问题,除非你真的找到了一个合适的副本(我搜索了将近两个小时所以找不到)。
将布尔值更改为false而不是true。
$('option:selected', this).attr('disabled', false);
I Just added a line here so after clicking delete button, option having val 0 got change.我刚刚在这里添加了一行,所以在单击删除按钮后,具有 val 0 的选项发生了变化。 What you should do is to change your html code and add val.
您应该做的是更改您的 html 代码并添加 val。
Here is the html code.这是 html 代码。
<select id="mySelect">
<option value="0" disabled selected>Please select one</option>
<option value="1">Volvooooo</option>
<option value="2">Saaaaaab</option>
<option value="3">Mercedeeeeeeees</option>
<option value="4">Audiiiiiii</option>
</select>
<div id="here">Nothing selected yet</div>
And here is the JS code,这是 JS 代码,
$('#mySelect').on('change', function (e){
if($('#mySelect>option:selected').val() != "0"){
$('#here').append('<div>' + $('#mySelect>option:selected').text() + '<button class="deleteButton" value="' + $(this).val() + '">DELETE</button> </div>');
$('#mySelect>option:selected').attr('disabled', true);
}
});
$("div").on("click", ".deleteButton", function(e) {
e.preventDefault();
$('#mySelect').val('0').trigger('change');
$('#mySelect>option[value=' + $(this).val() + ']').attr('disabled', false);
$(this).parent().remove();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.