Jquery: selecting items in a list and changing a trigger

It seems that I can not cause a change in my list after changing the selected value in the code. The change function works fine if you actually clicked the list. I searched the forums and none of the solutions seem to work for me. The following is sample code. I also created jsfiddle here . I want it to go into the change function when the code runs.

<form> <select class="selectOneListBox" id="CompanyId" multiple="multiple" name="CompanyId"> <option value="1">Test</option> <option value="2">Test 2</option> <option value="3">Test</option> </select> <form> 
 $(function () { id = 3 if (id > 0) { alert(id) $('#CompanyId').val(id).change(); } }); $("#CompanyId").change(function () { alert("change"); }); 
+4
source share
1 answer

You need to move your .change bind to the finished document block. I changed this in my jsfiddle, and he warned β€œchange” when the code called it, not just when clicked. http://jsfiddle.net/65wHW/2/

 $(function () { $("#ScriptId").change(function () { alert("change"); }); id = 8 if (id > 0) { alert(id) $('#ScriptId').val(id).change(); } }); 
+4
source

Source: https://habr.com/ru/post/1484975/


All Articles