javascript onclick alert not working in chrome

2020-01-29 11:03发布

<select name="history" id="history" >
 <option value="history 1" onClick="alert('h1');">history 1</option>
 <option value="history 2" onClick="alert('h2');">history 2</option>
 <option value="clearhistory" onClick="this.form.submit();" >Clear History</option>
</select>

could someone help me with this script? i am expecting that whenever the user clicks history 1 ..it will alert h1 the script works in firefox and IE but not in Chrome :(

5条回答
孤傲高冷的网名
2楼-- · 2020-01-29 11:25

In some browsers,

choosing same option second time,

no onchange-event throws.

This helps:

<select onchange="alert/*orwhateveryoudowith:*/(this.value);/*but set:*/this.selectedIndex=0">
<option disabled selected>Choose your Plan</option>
<option value=1>Plan 1</option>
<option value=2>Plan 2</option>
<option value=3>Plan 3</option>
</select>
查看更多
神经病院院长
3楼-- · 2020-01-29 11:28

Use onChange on the select instead of each option. here is the jsfiddle http://jsfiddle.net/tBjkg/

<select name="history" id="history" onChange="alert(this.value);">
 <option value="history1">history 1</option>
 <option value="history2">history 2</option>
 <option value="clearhistory">Clear History</option>
</select>

The alert(this.value) is referring to the value of the option within the select that is currently selected.

查看更多
地球回转人心会变
4楼-- · 2020-01-29 11:36

use onchange instead of onclick for select lists.

<select name="history" id="history" onchange="historyChanged(this);">
 <option value="history1">history 1</option>
 <option value="history2">history 2</option>
 <option value="clearhistory">Clear History</option>
</select>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">

function historyChanged() {
    var historySelectList = $('select#history');
    var selectedValue = $('option:selected', historySelectList).val();

    alert(selectedValue);

    if (selectedValue == 'clearHistory') {
        historySelectList.form.submit();
    }
}

$(function() {
    alert('my alert');
    $('select#history').change(historyChanged);
});

</script>
查看更多
我命由我不由天
5楼-- · 2020-01-29 11:41

If you want to use onClick for option element in Chrome, IE, etc., you must use short fix: set onChange attribute of select element to "this.options[this.selectedIndex].onclick()"

查看更多
够拽才男人
6楼-- · 2020-01-29 11:41

Although the selected solution works, but here is a more clearer and easy way to do this:

<select name="history" id="history" >
<option value="history 1">history 1</option>
<option value="history 2">history 2</option>
<option value="clearhistory">Clear History</option>
</select>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#history').on('change', function() {
  if ( this.value == 'history 1')
  {
   alert('h1');
  // You can also do anything here
  }
  if ( this.value == 'history 2')
  {
   alert('h2');
  // You can also do anything here
  }
  if ( this.value == 'clearhistory')
  {
   this.form.submit();
  // You can also do anything here
  } 
  });
});
</script>
查看更多
登录 后发表回答