警报选择框选项

时间:2018-01-07 12:36:38

标签: javascript jquery forms alert

我有一个包含多个选项的表单。

如果用户选择value="none"选项之一,我想显示一条提示消息“选择其中一个选项”

问题

我该怎么做?

<div id="search-box-wrapper-time">
  <form>
    <div>
      <select id="lang">
        <option value="next_week"> Next Week</option>
        <option value="this_weekend"> This Weekend</option>
        <option value="this_week"> This Week</option>
        <option value="tomorrow">Tomorrow</option>
        <option value="today">Today</option>
        <option value="none" selected>Select when</option>
      </select>
    </div>
  </form>
</div>

3 个答案:

答案 0 :(得分:0)

只需尝试 ('#lang').val() ,如下所示

$('#lang').on('change', function() {
  //console.log($(this).val())
  if ($(this).val() == 'none') {
    alert('Select one of the options')
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="search-box-wrapper-time">
  <form>
    <div>
      <select id="lang">
        <option value="next_week"> Next Week</option>
        <option value="this_weekend"> This Weekend</option>
        <option value="this_week"> This Week</option>
        <option value="tomorrow">Tomorrow</option>
        <option value="today">Today</option>
        <option value="none" selected>Select when</option>
      </select>
    </div>
  </form>
</div>

答案 1 :(得分:0)

试试这个:

exp start
  function selectOption(select) {
       if(select.options[select.selectedIndex].value == "none"){
             alert("Select one of the options");
       }
  }

答案 2 :(得分:-1)

如果您想阻止用户选择“无”。你可以用jquery这样做。

var values = $("#lang").val();
if (values == "none") 
{
alert("pick one");
}
else
{
// do this 
}