簡體   English   中英

在Firefox和IE中觸發Onclick事件,但在Chrome中沒有

[英]Onclick event firing in Firefox and IE but not in Chrome

我有以下javascript函數。

<script type="text/javascript">
function showhidefields(value,formobj,epsflag,respflag) {
  //alert(epsflag);
  if (epsflag==0 && respflag==4) {
    document.getElementById("tpnr").style.position = "static";
    document.getElementById("tpnr").style.top = "0px";
    formobj.tussenPersoonNotext.disabled =false;
    formobj.email.disabled =true;                  
  }
</script>

<select name="moduleidval" class="validate['required']">
   <option value="">-- Selecteer Proces--</option>
   <option onclick="showhidefields(this.value,this.form,'<?php echo 1; ?>');" 
   value="<?php echo $epsmodules; ?>"><?php echo 'MBO'; ?></option>
</select>

我的問題是,當我在的情況下,該選項點擊firefoxIE的onclick事件觸發,但在的情況下, 它在所有 。我試圖改變函數名也,但沒有運氣亘古不火

小提琴

提前致謝

當然,選項標簽上沒有click事件。

您可以收聽select標簽的onchange事件。

 <!-- HTML -->
<form id='myForm' action="#">
    <select id='mySelect' name="moduleidval" class="validate['required']">
      <option value="-">-- Selecteer Proces--</option>
      <option value="MBO">MBO</option>
    </select>
</form>

// JavaScript
document.getElementById('mySelect').onchange = function(){
    alert('value:'+this.value); 
    alert('form id:' + this.form.id);
}

在這里看演示

選項元素不會在所有瀏覽器中觸發click事件,您應該偏離依賴於此。 相反,您可以使用onchange()事件進行選擇 你可以使用這樣的東西

<html>
 <head>
  <script type="text/javascript">

   function changeFunc() {
    var selectBox = document.getElementById("selectBox");
    var selectedValue = selectBox.options[selectBox.selectedIndex].value;
    alert(selectedValue);
   }

  </script>
 </head>
 <body>
  <select id="selectBox" onchange="changeFunc();">
   <option value="1">Option #1</option>
   <option value="2">Option #2</option>`enter code here`
  </select>
 </body>`enter code here`
</html>

在你的情況下,你使用喜歡

<form id='myForm' action="#">
  <select id='mySelect' name="moduleidval" class="validate['required']" onchange="showhidefields(this.value,this.form,'0');">
   <option value="-">-- Selecteer Proces--</option>
   <option value="1"> 'Klantprocessen'</option>
</select>
</form>

很少有瀏覽器不支持選項標簽的onclick事件。 這是參考 在此輸入圖像描述

所以你可以選擇改變選擇標簽。

您可以直接在指定的函數中傳遞值,例如:

<select onchange="trig(value);">

然后可以在trig函數中使用該值。

function trig(val) {
    //do foo
    alert(val);
}

簡短而簡單。

Chrome中的一個工作演示: http//jsfiddle.net/amarprabhu/9C3VU/

檢查是否在您的Chrome中啟用了javascript。

Select Customize and control Google Chrome to the right of the address bar

From the drop-down menu, select Settings

At the bottom of the page, click Show advanced settings…

Under Privacy, click the Content settings… button

Under the JavaScript heading, select the Allow all sites to run JavaScript radio button

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM