2017-08-04 37 views
1

我有這個HTML網站廣東話提交HTML表單後,使用js腳本

<html> 
<head> 
    <link rel="stylesheet" href="~/Content/index.css" /> 
    <link rel="stylesheet" href="~/Scripts/fancyBox/source/jquery.fancybox.css" type="text/css" media="screen" /> 
    <meta name="viewport" content="width=device-width" /> 
    <script type="text/javascript" src="~/Scripts/prototype.js"></script> 
    <script type="text/javascript" src="~/Scripts/scriptaculous.js?load=effects"></script> 
    <script type="text/javascript" src="~/Scripts/Scale.js"></script> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
    <script type="text/javascript" src="~/Scripts/fancyBox/source/jquery.fancybox.pack.js"></script> 
    <title>Countries</title> 
</head> 
<body> 
    <div> 
     @foreach (var country in Model) 
     { 
      <a class="fancybox"> 
       <div class="tooltip"> 
        @using (Html.BeginForm("ChangeCulture", "Home", new { lang = country.Culture })) 
        { 
         <!-- onclick="zoomIn('@country.Culture')"--> 
         <input id="@country.Culture" class="@country.Sprite" type="image" name="submit" src="//" /> 
         <span class="tooltiptext">@country.Country</span> 
        } 
       </div> 
      </a> 
     } 
    </div> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     $(".fancybox").fancybox({ 
      afterShow : function() { 
       this.find("form").submit(); 
      } 
     }); 
    }) 
</script> 
</body> 
</html> 

底線是,隨着提示國家的國旗顯示在屏幕上,並通過按下它是需要改變的語言提供該國使用的語言的工具提示。我這樣做了,現在我需要在收到帶有標誌的圖片後增加整個屏幕,然後語言會改變。我發現了fancybox腳本,但插入後,表單不發送,選中的標記出現在中心,但之後沒有任何反應,可能是什麼問題?我還沒有寫ASP和JS

之前點擊圖片後,我得到了一個錯誤

Uncaught TypeError: this.find is not a function 
    at Object.afterShow (Index:119) 
    at Function.trigger (jquery.fancybox.pack.js:17) 
    at HTMLDivElement._afterZoomIn (jquery.fancybox.pack.js:33) 
    at HTMLDivElement.d.complete (jquery-latest.min.js:4) 
    at j (jquery-latest.min.js:2) 
    at Object.fireWith [as resolveWith] (jquery-latest.min.js:2) 
    at i (jquery-latest.min.js:4) 
    at m.fx.tick (jquery-latest.min.js:4) 

我的方法

public ActionResult ChangeCulture(string lang) 
     { 
      var returnUrl = this.Request.UrlReferrer?.AbsolutePath; 
      var cookie = this.Request.Cookies["lang"]; 
      if (cookie != null) 
      { 
       cookie.Value = lang; 
      } 
      else 
      { 
       cookie = new HttpCookie("lang") 
       { 
        HttpOnly = false, 
        Value = lang, 
        Expires = DateTime.Now.AddYears(1) 
       }; 
      } 
      this.Response.Cookies.Add(cookie); 
      return this.Redirect(returnUrl ?? "Index"); 
     } 
+0

有你在瀏覽器的控制檯中發現JS錯誤? '$(this).closest(「form」)。submit()'通常工作。 –

+0

是的,我得到了一個錯誤,並已將其置於問題 – SmiLe

+0

嘗試'console.log(this)'在$(「。fancybox」)。fancybox'中。你得到了什麼?我認爲你應該使用'$(this).find(「form」)。submit();'而不是。 –

回答

0

this.find("form")this不是$(".fancybox")這裏。

嘗試使用$('.fancybox form')代替。

1

你應該用戶jQuery選擇

<script type="text/javascript"> 
    $(document).ready(function() { 
     $(".fancybox").fancybox({ 
      afterShow : function() { 
       $(".fancybox").submit(); 
      } 
     }); 
    }) 
1

錯誤坐落在此聲明:

this.find("form").submit(); 

你試圖調用find()方法在普通JS對象中this包裝,其中功能屬於一個jQuery對象。正確的方法是使用jQuery對象是這樣的:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $(".fancybox").fancybox({ 
      afterShow : function() { 
       // alternative: use $(this).closest("form").submit(); 
       $(this).find("form").submit(); 
      } 
     }); 
    }); 
</script> 

此外,在控制器操作的重定向的方法應該是這樣的下面,因爲Controller.Redirect需要到另一個頁面的完整路徑或網址重定向:

public ActionResult ChangeCulture(string lang) 
{ 
    var returnUrl = this.Request.UrlReferrer?.AbsolutePath; 

    // cookie settings here 

    if (string.IsNullOrEmpty(returnUrl)) 
    { 
     return this.RedirectToAction("Index"); 
    } 

    return this.Redirect(returnUrl); 
}