简体   繁体   English

当选择框中的选项第二次选择时,如何使用javascript显示警报

[英]How to show an alert using javascript when the option from select box choose secondly

I have grid which has 'N' number of rows. 我有行数为“ N”的网格。 I have a select box on each row with three options. 我在每行上都有一个选择框,其中包含三个选项。

<tr>
 <td>
   <select id="1">
    <option>YES</option>
    <option>NO</option>
    <option>IGNORE</option>
   </select>
 <td>
</tr>
<tr>
 <td>
   <select id="2">
    <option>YES</option>
    <option>NO</option>
    <option>IGNORE</option>
   </select>
 <td>
</tr>
<tr>
 <td>
   <select id="3">
    <option>YES</option>
    <option>NO</option>
    <option>IGNORE</option>
   </select>
 <td>
</tr>

I would like to show an alert when the user selects IGNORE secondly. 当用户第二次选择IGNORE时,我想显示一个警报。 I dont want to show the alert when the user select 'IGNORE' on first time. 当用户第一次选择“ IGNORE”时,我不想显示警报。

Let us consider select ID#1, i dont want to show an alert if the user choose 'IGNORE' or 'YES' for the first time. 让我们考虑选择ID#1,如果用户第一次选择“ IGNORE”或“ YES”,我不想显示警报。 But if the user has already clicked on 'YES' or 'NO' and then tries to click on 'IGNORE' next time then i want to show an alert. 但是,如果用户已经单击“是”或“否”,然后下次尝试单击“ IGNORE”,那么我想显示警报。

This function has to work on all select box. 此功能必须在所有选择框上起作用。 Can somebody help to resolve this. 有人可以解决这个问题吗?

You can create a javascript map that maintains count of how many time a value in dropdown is selected. 您可以创建一个JavaScript映射,以维护选择下拉列表值的次数。 If that count is more than 1 for a dropdown, you can show alert. 如果下拉菜单的计数大于1,则可以显示警报。

<tr>
 <td>
 <select id="1" onchange="updateSelectionCount('1')">
  <option>YES</option>
  <option>NO</option>
 <option>IGNORE</option>
</select>
<td>
</tr>
<tr>
<td> 
<select id="2" onchange="updateSelectionCount('2')">
<option>YES</option>
<option>NO</option>
<option>IGNORE</option>
</select>
 <td>
</tr>
<tr>
<td>
 <select id="3" onchange="updateSelectionCount('3')">
<option>YES</option>
<option>NO</option>
<option>IGNORE</option>
 </select>
<td>
</tr>
<script>
var map = new Object();
function updateSelectionCount(comboId){
    var e = document.getElementById(comboId);
    var selectedValue = e.options[e.selectedIndex].value;
    if(selectedValue=='IGNORE'){
        if(map[comboId]==1){
            alert('selected second time');
        }else{
            map[comboId]=1;
        }
    }else{
        map[comboId]=1;
     }

}
</script>

If you want to use jquery you can also do this: 如果要使用jQuery,也可以执行以下操作:

<script type="text/javascript">
    $( document ).ready(function() {
       $('select').on('change', function() {

            if (typeof this.count == 'undefined')
            {
                this.count = 0;
            }
            else{
                if (this.count>=0 && this.value=="2")
                {
                    alert("my Warning")
                }
                this.count += 1
            }
        })
    });
  </script>
   <select>
    <option value="0">YES</option>
    <option value="1">NO</option>
    <option value="2">IGNORE</option>
   </select>

   <select>
    <option value="0">YES</option>
    <option value="1">NO</option>
    <option value="2">IGNORE</option>
   </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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