admin管理员组文章数量:1403522
Is there a specific event for when a user selects an item in a dropdown box? I am creating a fairly large form that uses multiple dropdown boxes. On load all but the first are disabled, as the user selects the first option the options in the second box change using AJAX and I want to remove the attribute disabled.
$('#site_id').on('change', function(event) {
$('#access_type_id').removeAttr('disabled');
});
The trouble I am having is that .change()
seems to be fired when the options in a dropdown change making following dropdown boxes enabled before I actually want them to be. Is there an alternative event I can use in place of change to only "enable" a form field once the user has made their selection in the previous dropdown?
UPDATE
I managed to solve this by not using the change event at all and placing my removeAttr in the success section of my AJAX call. An example is below. This allowed me to make the "next" dropdown enabled at the time as populating its options.
$('#access_speed_id').on('select2-blur', function(event){
$.ajax({
async: true,
data: $('#access_speed_id').serialize(),
dataType: 'html',
success: function(data, textStatus) {
$('#cdr_id').removeAttr('disabled');
$('#cdr_id').html(data);
},
type: 'post',
url: '/services/specificCdrs'
});
});
Is there a specific event for when a user selects an item in a dropdown box? I am creating a fairly large form that uses multiple dropdown boxes. On load all but the first are disabled, as the user selects the first option the options in the second box change using AJAX and I want to remove the attribute disabled.
$('#site_id').on('change', function(event) {
$('#access_type_id').removeAttr('disabled');
});
The trouble I am having is that .change()
seems to be fired when the options in a dropdown change making following dropdown boxes enabled before I actually want them to be. Is there an alternative event I can use in place of change to only "enable" a form field once the user has made their selection in the previous dropdown?
UPDATE
I managed to solve this by not using the change event at all and placing my removeAttr in the success section of my AJAX call. An example is below. This allowed me to make the "next" dropdown enabled at the time as populating its options.
$('#access_speed_id').on('select2-blur', function(event){
$.ajax({
async: true,
data: $('#access_speed_id').serialize(),
dataType: 'html',
success: function(data, textStatus) {
$('#cdr_id').removeAttr('disabled');
$('#cdr_id').html(data);
},
type: 'post',
url: '/services/specificCdrs'
});
});
Share
Improve this question
edited Oct 16, 2022 at 11:41
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Jul 31, 2013 at 9:16
theMojoWilltheMojoWill
761 gold badge2 silver badges9 bronze badges
1
- its a bit confusing what do you actually want? can you set up a jsfiddle or explain it in more detail – Mandeep Jain Commented Jul 31, 2013 at 9:35
1 Answer
Reset to default 3Try Below code:
<select id="drop1">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<select id="drop2"></select>
Jquery
$(document).ready(function(){
$("#drop2").attr("disabled", true);
$("#drop1").change(function(){
$("#drop2").attr("disabled", false);
//Put your ajax code here and bind like below code
$("#drop2").append(new Option("option text", "value"));
$("#drop2").append(new Option("xxx","1"));
});
});
Demo: http://jsfiddle/J5kmz/
本文标签: javascriptjQuery dropdown select eventStack Overflow
版权声明:本文标题:javascript - jQuery dropdown select event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744369620a2602954.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论