Heim > Web-Frontend > js-Tutorial > Hauptteil

JavaScript调用传递变量参数的相关问题及解决办法_javascript技巧

WBOY
Freigeben: 2016-05-16 15:34:27
Original
1185 Leute haben es durchsucht

举例

有一个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>
Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!