2013-02-24 107 views
0

在窗體上使用jquery mobile進行刷新。按下該按鈕後,頁面會刷新,但腳本(頁面上)無法運行(單擊提交時的操作)。下面是頁面的外觀:頁面刷新後腳本失敗

@using (Html.BeginForm()) 
{ 
    <input type="submit" id="refresh" name="actionType" value="Refresh" /> 
    <input type="submit" id="submit" name="actionType" value="Submit" /> 
} 

@Scripts.Render("~/bundles/jquery")  

<script type="text/javascript"> 
    $('#submit').click(function() { 
    alert('Here'); 
    }); 
</script> 

刷新實現爲:

[HttpPost] 
    public ActionResult Index(string actionType) 
    { 
     if (actionType == "Refresh") 
     { 
      return RedirectToAction("Index"); 
     } 
     ... 
    } 

在此先感謝您的幫助!

回答

0

我不知道jQuery Mobile的頁面刷新如何影響你的腳本。也許它確實有事情和提交IDS ...

但是你可以嘗試使用不同的選擇:如訂閱在具有類型的input元素提交:

<script type="text/javascript"> 
    $('input[type="submit"]').click(function() { 
    alert('Here'); 
    }); 
</script> 
+0

它的工作原理,謝謝!但爲什麼選擇'#submit'沒有刷新工作? – Gerard 2013-02-24 14:10:47

+0

說實話我不知道...你提到你使用jQuery手機,所以我想它與jQuery手機相關,儘管你的示例代碼不包含任何與jQuery手機相關的部分。 – nemesv 2013-02-24 14:22:02

0

總是試圖做任何你想做內的document.ready():

$(document).ready(function() { 
    $('#submit').click(function() { 
    alert('Here'); 
    }); 
}); 
0

我建議你訂閱形式的.submit()事件,而不是提交按鈕的.click()事件:

<script type="text/javascript"> 
    $('form').submit(function() { 
    alert('Here'); 
    }); 
</script>