2013-12-11 35 views
1

對不起壞標題。我有一個帶有輸入字段的簡單表單,我知道每次提交表單時,傳遞的參數數量都是1。當一個參數通過重新加載被評估爲true,並且如果重新加載爲true,頁面刷新。問題是,如果我不想刷新頁面。如果我想要在某些情況下重新加載以評估爲false,該怎麼辦?是否有一些參數可以傳遞給userInput函數來評估某些場景爲false?有任何想法嗎?我通過代碼示例學到了最好的東西,請在適用的地方加入。我也有一個小提琴去這裏:jQuery - 根據函數參數更改參數個數

http://jsfiddle.net/JuG48/2/

<form id="myForm"> 
    <input id="address" name="address" value="address"> 
    <input type="submit" name="submit" value="submit"> 
</form> 

function userInput(userLocation, input){ 
    $('#myForm').on('submit', function(e){ 
    reload = arguments.length > 1 ? arguments[1] : true; 
    if (reload) location.reload(); 
    console.log(reload); 
}); 
} 

$(document).ready(function() {  
    userInput(); 
}); 
+0

對不起,我沒有得到你想要的東西.. –

+0

我試圖找出是否有一種方法可以讓我在一個參數的函數,將返回在某些情況下假傳。所以我可以做一些像** userInput(false)**這只是一個猜測。 –

+0

我認爲你必須設置一些cond它將在函數內部爲false,例如:function somemethod(p1,p2){reload =(arguments.length <1?false:(arguments.length> 1?arguments [1]:true );} – Neha

回答

1

我覺得你的情況最快的解決辦法是garlic.js

http://garlicjs.org/

<script type="text/javascript"> 
    $('form').garlic(); 
</script> 

本地解決方案將是

$('#myForm').submit(function() { 
reload = arguments.length > 1 ? arguments[1] : true; 
if (reload) location.reload(); 
console.log(reload); 
e.preventdefault(); 
}); 
+0

感謝@ProllyGeek!我正在尋找更多的本地解決方案,似乎在伎倆。 –

+0

@DelmonYoung沒有問題,歡迎您。 – ProllyGeek

0

假設你想在userInput沒有參數的()方法調用reload = false所以這樣做...

function userInput(userLocation, input){ 
$('#myForm').on('submit', function(e){ 

    reload = (userLocation==undefined && input==undefined)? false 
    :(arguments.length > 1 ? arguments[1] : true);  
    console.log(reload); 
    if (reload) location.reload(); 
    else return false;  
    }); 
} 

$(document).ready(function() {  
userInput(); 
}); 

Fiddle demo