简体   繁体   中英

drop down menu selection using console doesn't trigger event

I want to set value for field with help of console,but when I am doing this it is not triggering the event which should occur when I select option from drop down list.It only shows selected value in drop down menu field,but event doesn't occur.

I am using following query on console:

document.getElementsByClassName("input-text")[5].value="string:100#2#######" 

Following is code for HTML select:-

<select class="input-text ng-pristine ng-valid ng-touched" ng-model="selectedSname" ng-show="snames != null &amp;&amp; (name712 != null &amp;&amp; name712 != '')" ng-options="sname.surveypins as sname.fullname for sname in snames">
  <option selected="selected"></option>
  <option label="1" value="string:55#9####### ">abc</option>
  <option label="2" value="string:100#2####### ">xyz</option>
</select>

You have to call the onchange method manually like:

HTML:

<select id="test">
  <option value="1">1</option>
  <option value="2">2</option>
</select>

JS:

document.getElementById('test').onchange = function () {
    alert('changed');
}

document.getElementById('test').value = "2";
document.getElementById('test').onchange();

A working JSFiddle is here


And because you are using AngularJS, we have to choose the options by angular way like this:

$scope.selectedSname = "string:100#2#######";

So angular will catch your change and fire the digest loop, which will fire the change event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM