如何将文本放在选择类中?

时间:2019-03-23 03:57:44

标签: javascript angularjs dom

我正在尝试将文本放入选择类。

这是下面的代码;

    <div class="col-xs-12 col-sm-9">        
<select class="form-control col-l10 col-m7 col-s7 esw-select ng-pristine ng-invalid ng-invalid-required ng-touched" ng-class="{'is-invalid': shipping_subform.$submitted &amp;&amp; shipping_subform.shippingRegion.$invalid}" id="Shipping_Territory" required="" ng-options="region.code  as region.name for region in regions" ng-model="userOrder.Shipping.Address.Region" name="shippingRegion" tooltip="Please select a province" tooltip-toggle=" shipping_subform.$submitted===true &amp;&amp; shipping_subform.shippingRegion.$invalid===true" esw-google-analytics-blur="">

<option value="" class="ng-binding">Select a province</option>
<option value="0" label="Ontario">Ontario</option>
<option value="1" label="British Columbia">British Columbia</option><option value="2" label="Manitoba">Manitoba</option>
<option value="3" label="Alberta">Alberta</option>
<option value="4" label="New Brunswick">New Brunswick</option>
<option value="5" label="Newfoundland and Labrador">Newfoundland and Labrador</option>
<option value="6" label="Northwest Territories">Northwest Territories</option>
<option value="7" label="Nova Scotia">Nova Scotia</option><option value="8" label="Nunavut">Nunavut</option>
<option value="9" label="Prince Edward Island">Prince Edward Island</option>
<option value="10" label="Quebec">Quebec</option>
<option value="11" label="Saskatchewan">Saskatchewan</option>
<option value="12" label="Yukon">Yukon</option>
</select>        <!-- ngIf: shipping_subform.$submitted && shipping_subform.shippingRegion.$invalid -->    
</div>

我尝试了以下可行的方法;但是,当我单击“提交”按钮时,提示您选择一个省。

这有效,但是在单击“提交”按钮时实际上不起作用;

document.querySelector('#Shipping_Territory [label= Ontario]').selected = true;

据说我必须使用角度,但我不知道要在triggerHandler中放置什么。

 var e = document.getElementById("Shipping_Territory");
  e.value = "Ontario";
  var $e = angular.element(e);
  $e.triggerHandler('??');

我已经在triggerHandler中尝试过,进行更改,更改,选择,选择,但是只是将选择下拉列表变为空白。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

为标签添加ID

<select id="selectedOption" class="form-control col-l10 col-m7 col-s7 esw-select ng-pristine ng-invalid ng-invalid-required ng-touched" ng-class="{'is-invalid': shipping_subform.$submitted &amp;&amp; shipping_subform.shippingRegion.$invalid}" id="Shipping_Territory" required="" ng-options="region.code  as region.name for region in regions" ng-model="userOrder.Shipping.Address.Region" name="shippingRegion" tooltip="Please select a province" tooltip-toggle=" shipping_subform.$submitted===true &amp;&amp; shipping_subform.shippingRegion.$invalid===true" esw-google-analytics-blur="">

并使用以下方法获取selectedOption的索引:-

document.getElementById("selectedOption").options.selectedIndex     

我希望对您有帮助!

答案 1 :(得分:0)

我成功地使用了以下内容;

var e = document.getElementById("Shipping_Territory");
  e.value = 2;
  var $e = angular.element(e);
  $e.triggerHandler('change');