The onclick event of the option in the select is invalid

html:

<select id="pageSelect">

  <option value="1" selected onclick="openNewPage()">页面1</option>

  <option value="2" onclick="openNewPage()">页面2</option>

</seelct>

js:

<script type="text/javascript">

function openNewPage(){

  var value = $("# pageSelect option:select").value;

  if(value == "1"){

    // pop up page 1

  }else{

    // pop up page 2

  }

}

</script>

The above, it can run normally in high version browsers, but in low version browsers, the onclick event of option is invalid, because low version browsers do not support the onclick event of option

Solution:

The onclick event can be added to the select instead of the onchange event, because the content value of the option must be changed to trigger the onchange event;

However, adding the onclick event to the select will cause the onclick event to be triggered when you click on the selection box and not select an option. Therefore, after adding the onclick event, it should also be processed in js

html:

<select id="pageSelect"  onclick="openNewPage(this)">

  <option value="1" selected>页面1</option>

  <option value="2">页面2</option>

</seelct>

js:

<script type="text/javascript">

//Define a boolean variable to control the click event of the selection box

var isSelect=true;

function openNewPage(value){

  var optionSelect= value.options[value.selectedIndex];

  if(value.value == optionSelect.value){

    isSelect = !isSelect

  } else {

    isSelect = true;

  }

  if(isSelect == true){

    if(value == "1"){

      // pop up page 1

    }else{

      // pop up page 2

    }

  }

}

</script>

Part of the content is borrowed from https://blog.csdn.net/svtme/article/details/5149544

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324899428&siteId=291194637