javascript onclick alert not working in chrome

旧时模样 提交于 2019-12-17 13:08:28

问题


<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 :(


回答1:


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>



回答2:


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()"




回答3:


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:


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>



回答5:


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>


来源:https://stackoverflow.com/questions/4340690/javascript-onclick-alert-not-working-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!