JavaScript調用傳遞變數參數的相關問題及解決辦法,javascript相關問題

來源:互聯網
上載者:User

JavaScript調用傳遞變數參數的相關問題及解決辦法,javascript相關問題

舉例

有一個js方法,接收參數:

複製代碼 代碼如下:
function f1(myValue){ alert(myValue); }

有一個變數:

複製代碼 代碼如下:
var passValue="Hello World";

在調用這個方法的時候(我是出現在Ajax提交的時候):

@Ajax.ActionLink("文本","控制器",new{參數},new AjaxOptions(){ HttpMethod="post",OnSuccess="f1(PassValue)" })

這裡注意最後的OnSuccess,如果直接把變數丟進去,會把變數認為是一個字串

如果改成OnSuccess="f1("+PassValue+")"也不行

搜了一下是需要逸出字元

OnSuccess="f1('"+PassValue+"')"

這樣就沒問題了

不過上面調用Ajax的時候沒注意,這裡只是為了給非同步呼叫方法f1()傳參數

所以就不用@Ajax了 改成普通A標籤就可以了 不然會調用兩次控制器

ps:js將方法作為參數調用

<!DOCTYPE html><html><head>  <meta http-equiv="content-type" content="text/html; charset=utf-8" />  <title>js調用</title>    <script src="cssjs/jquery.js" type="text/javascript"></script>  <script type="text/javascript">    $().ready(function () {      $.dialog = function (settings) {        if ($.isFunction(settings.okCallback)) {          if (settings.height == null) {            if (settings.okCallback.apply() != false) {              alert("1");            }          } else {                        if (settings.okCallback.call(this, settings.height) != false) {              alert("2");            }                        /*            if (settings.okCallback.apply(this, arguments) != false) {              alert("2");            }            */          }        }      }    });      </script>  <script type="text/javascript">    $(function () {      $.dialog({        okCallback: print,        height: {data:"你好"}      });    });  function print(ee1) {    alert("print(ee1)");        alert(ee1.data);        /*    alert(ee1.height.data);    */  /*  function print(a, b, c, d) {  alert(a + b + c + d);  }  function example(a, b, c, d) {  //用call方式借用print,參數顯式打散傳遞  print.call(this, a, b, c, d);  //用apply方式借用print, 參數作為一個數組傳遞,  //這裡直接用JavaScript方法內本身有的arguments數組  print.apply(this, arguments);  //或者封裝成數組  print.apply(this, [a, b, c, d]);  }  //下面將顯示"背光指令碼"  example("背", "光", "腳", "本");   */  </script></head><body> </body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.