当 select 框中的选项第二次选择时,如何使用 javascript 显示警报
How to show an alert using javascript when the option from select box choose secondly
我的网格有 'N' 行数。我在每一行都有一个 select 框,其中包含三个选项。
<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>
我想在用户 select 第二次忽略时显示警告。我不想在用户第一次 select 'IGNORE' 时显示警报。
让我们考虑 select ID#1,如果用户第一次选择 'IGNORE' 或 'YES',我不想显示警报。但是,如果用户已经单击了 'YES' 或 'NO',然后下次尝试单击 'IGNORE',那么我想显示一个警报。
此功能必须对所有 select 框起作用。有人可以帮助解决这个问题。
您可以创建一个 javascript 地图来记录下拉列表中的值被选中的次数。如果下拉列表的计数大于 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>
如果你想使用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>
我的网格有 'N' 行数。我在每一行都有一个 select 框,其中包含三个选项。
<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>
我想在用户 select 第二次忽略时显示警告。我不想在用户第一次 select 'IGNORE' 时显示警报。
让我们考虑 select ID#1,如果用户第一次选择 'IGNORE' 或 'YES',我不想显示警报。但是,如果用户已经单击了 'YES' 或 'NO',然后下次尝试单击 'IGNORE',那么我想显示一个警报。
此功能必须对所有 select 框起作用。有人可以帮助解决这个问题。
您可以创建一个 javascript 地图来记录下拉列表中的值被选中的次数。如果下拉列表的计数大于 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>
如果你想使用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>