2015-01-12 72 views
-2

事件。防止默認的工作在鉻但不工作在Firefox中我已經嘗試了兩種方法,但不工作我已經搜索了所有可能的解決方案,但它沒有工作。 如果你想讓我發佈我的所有代碼,我會發布它,如果你沒有發現錯誤。在每個警報頁刷新後,它不應該刷新停止刷新我用防止默認在Chrome瀏覽器中正常工作,但不在Firefox中

function validate_calc(e){ 
    event.preventDefault(); 



function validate_calc(event){ 
event.preventDefault(); 

if ($('input[name=water]:checked').length > 0) { 

    var $radio = $('input[name=water]:checked'); 
    var id = $radio.attr('id'); 
    if(id == 'water'){ 
    if($('input[name=rad_waters]:checked').length > 0){ 
     if($('input[name=water_types]:checked').length > 0){ 
      $value = jQuery.trim($(".cal_input").val()); 
      if($value == ""){ 
      alert('Please Insert Values'); 
      return; 
      } else { 
       if($.isNumeric($(".cal_input").val())){ 
       var form_data = { 
       length: $('input[name="length"]').val(), 
       width: $('input[name="width"]').val(), 
        }; 

        jQuery.ajax({ 
        type  : "POST", 
        data  : form_data, 
        url   : baseurl+'solar_calculator/calculate_area', 
        success  : function(msg){ 
        alert(msg);return; 
        } 
        }); 
       }else{ 
        alert('Value should be in Numbers.'); 
        return; 
        } 
      } 
     }else{ 
      alert('Select a Sub water type'); 
      return; 
     } 
     }else{ 
     alert('Select a water type.'); 
     return; 
     } 
    } else { 
    $value = jQuery.trim($(".cal_input").val()); 
      if($value == ""){ 
      alert('Please Insert Values'); 
      return; 
      } else { 
       if($.isNumeric($(".cal_input").val())){ 
        var form_data = { 
       length: $('input[name="length"]').val(), 
       width: $('input[name="width"]').val(), 
        }; 
        jQuery.ajax({ 
        type  : "POST", 
        data  : form_data, 
        url   : baseurl+'solar_calculator/calculate_area', 
        success  : function(msg){ 
        alert(msg);return; 
        } 
        }); 
       }else{ 
        alert('Value should be in Numbers.'); 
        return; 
        } 
      } 
    } 
    } else { 
alert('Please Select a water type'); 
return; 
} 
} 

或者我用這個方法甚至沒有工作

function validate_calc(e){ 
    e.preventDefault(); 

..........

回答

1

事件 - 事件預防默認不會在功能工作。

ID爲 做得一樣

$('#myid').click(function(event){ 
event.preventDefault; 
alert('am i working....'); return 
}) 
2

你傳入事件中的 「e」 參數該函數,然後用「事件」引用它?正確的語法應該是

function validate_calc(e){ 
    e.preventDefault(); 
    // ... 
} 

此外,爲了您的代碼正常工作,您必須將正確的事件值傳遞給函數,否則它將無法工作。

你的代碼在chrome而不是firefox中工作的原因是你正在使用超級全局變量「event」,它是在chrome中定義的,但不是在firefox中定義的。你可以在這裏閱讀更多Unexpected access to "event" variable across browsers?

〔實施例的代碼,將工作:

$("form").submit(function(e) { 
    validate_calc(e); 
} 

示例代碼將無法工作:

$("form").submit(function() { 
    validate_calc(); 
} 
+0

我已經與功能validate_calc(E){e.preventDefault)這兩種方法(試了;不工作? –

+0

你傳遞的是什麼「e」值?它應該是觸發事件。請提供js小提琴示例。 – shaddy

+0

Shaddy我已經發布了我所有的js腳本,現在你可以看到什麼問題了,我無法撥動它,它動態地用php編寫。 –