2014-06-15 45 views
0

我已經做了一個php表單,其中我想顯示一個加載gif,如果某些條件得到滿足。這是一個可讀的僞代碼/我需要在jQuery中使用的邏輯示例。加入兩個簡單的jQuery條件

if (file_input_is_set && submit_button_is_clicked) { 
    <div class="progress-bar">tada...loading_gif</div> 
    } 

我有這兩個單獨工作的代碼。 第一個代碼顯示設置文件輸入時的加載gif。

jQuery().on('change', 'input[type="file"]', function(e) { 

二碼顯示加載GIF被點擊的形式提交按鈕時。

jQuery("input[name='profile_submit']").click(function (e) { 

因爲我幾乎一無所知jQuery的,我需要幫助把這些事件一起。請原諒我,如果我的術語是錯誤的。這是我嘗試過的不起作用,甚至在我按下提交按鈕之前顯示加載gif。所以我需要一個像php的&&運算符那樣工作的函數,我不知道你在jquery中是如何實現的。

jQuery().on('change', 'input[type="file"]', function(e) { 
    jQuery("input[name='profile_submit']").click(function (e) { 
    //jQuery('#submit-button').hide(); 
    jQuery('.progress-bar').fadeIn(1000); 
    }); 
    }); 

注意:您看到的代碼有很多jQuery是由於WordPress的原因(其中形式使用),因爲它需要它是在沒有任何衝突模式。


轉述問題(從@webkit提示):我檢查,如果 file_input_is_set /有效時提交事件觸發怎麼辦?

+1

檢查條件和檢查事件(ch ange,click ..)你可以將事件委託給一個處理程序,或者在提交事件觸發器時也檢查輸入是否有效 – webkit

回答

1

從上次操作開始,表單提交。然後檢查文件是否存在於文件輸入:

jQuery("input[name='profile_submit']").click(function (e) { 
     if(jQuery('input[type="file"]').val() != ''){ 
     jQuery('.progress-bar').fadeIn(1000); 
     } 
}); 

的jsfiddle演示:http://jsfiddle.net/nDNP5/

+0

我在響應中添加了一個可用的JSFiddle。 – bloodyKnuckles

+0

它的工作,是的。我不得不刷新緩存以查看更改。對不起,我以前沒有意識到。 – gurung

1

,你可以這樣做:

jQuery("input[name='profile_submit']").click(function (e) { 
    // check to see if file is valid 
    if (jQuery('input[type="file"]').val() != "") { 
     // load 
     jQuery('.progress-bar').fadeIn(1000); 
    } 
}); 

或者這樣:(僅在已選定的文件將您激活提交btn)

jQuery('input[type="file"]').on('change', function (e) { 
    // check to see if file is valid 
    if (jQuery('input[type="file"]').val() != "") { 
     jQuery("input[name='profile_submit']").off().on('click', function() { 
      jQuery('.progress-bar').fadeIn(1000); 
     }); 
    } else { 
     jQuery("input[name='profile_submit']").off() 
    } 
}); 
+0

感謝您的努力 – gurung