最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - If option selected do something - Stack Overflow

programmeradmin2浏览0评论

I have form. I want to do something, if the certain option is selected. I try this

if (document.getElementById('order-select').value == "Услуга") {
  alert("blabla");
};
<script src=".1.1/jquery.min.js"></script>
<select id="order-select">
  <option value="Услуга" class="item-1">Услуга</option>
  <option value="Строительный проект" class="item-2">Строительный проект</option>
</select>

I have form. I want to do something, if the certain option is selected. I try this

if (document.getElementById('order-select').value == "Услуга") {
  alert("blabla");
};
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="order-select">
  <option value="Услуга" class="item-1">Услуга</option>
  <option value="Строительный проект" class="item-2">Строительный проект</option>
</select>

Problem : I want to alert every time, when option is selected. Need help

Share Improve this question asked Nov 23, 2015 at 14:46 AlexAlex 551 silver badge7 bronze badges 1
  • Show your event handler. (or add one if you haven't) developer.mozilla/en-US/docs/Web/Events – Shilly Commented Nov 23, 2015 at 14:49
Add a ment  | 

1 Answer 1

Reset to default 5

You need to attach an event listener for the change event.

When the event is fired, you can access the element through the target object on the event object.

document.getElementById('order-select').addEventListener('change', function (e) {
  if (e.target.value === "Услуга") {
    alert('Selected');
  }
});
<select id="order-select">
  <option class="item-1">--Select--</option>
  <option value="Услуга" class="item-2">Услуга</option>
  <option value="Строительный проект" class="item-3">Строительный проект</option>
</select>

Updated Example

document.getElementById('order-select').addEventListener('change', function (e) {
  if (e.target.value === "Услуга") {
    alert('Selected');
  }
});
发布评论

评论列表(0)

  1. 暂无评论