Home >Web Front-end >JS Tutorial >Problems and solutions related to passing variable parameters in JavaScript calls_javascript skills

Problems and solutions related to passing variable parameters in JavaScript calls_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:34:271268browse

Example

There is a js method that receives parameters:

Copy code The code is as follows:

function f1(myValue){ alert(myValue); }

has a variable:

Copy code The code is as follows:

var passValue="Hello World";

When calling this method (I appear when Ajax submits):

@Ajax.ActionLink("Text","Controller",new{parameter},new AjaxOptions(){ HttpMethod="post",OnSuccess="f1(PassValue)" })

Pay attention to the last OnSuccess here. If you throw the variable directly, the variable will be considered a string

It won’t work if you change it to OnSuccess="f1(" PassValue ")"

After searching, it turns out that escape characters are needed

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

That’ll be fine

But I didn’t pay attention when calling Ajax above. This is just to pass parameters to the asynchronous calling method f1()

So there is no need for @Ajax. Just change it to a normal A tag. Otherwise, the controller will be called twice

ps:js calls the method as a parameter

<!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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn