Problems related to the JavaScript call passing variable parameters and solutions

  • 2020-09-16 07:22:37
  • OfStack

For example,

There is an js method that receives parameters:


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

There is one variable:


var passValue="Hello World";

At the time this method is called (I appear when Ajax commits) :

Ajax. ActionLink(" text "," controller ",new{parameter},new AjaxOptions(){HttpMethod="post",OnSuccess="f1(PassValue)"})

Note here that the last OnSuccess, if you drop the variable directly into it, will be treated as a string

Not if OnSuccess="f1("+PassValue+")"

Search 1 is the need to escape characters

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

That should be all right

I didn't notice when I called Ajax above, but just to pass arguments to the asynchronous call method f1()

So instead of using @Ajax you can just use the regular A tag otherwise you're going to call the controller 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 call </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:" hello "}
      });
    });
  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) {
  // with call Way to use print, Parameter explicit scatter transfer 
  print.call(this, a, b, c, d);
  // with apply Way to use print,  Parameters as 1 Array pass ,
  // I'm just going to use JavaScript It's in the method itself arguments An array of 
  print.apply(this, arguments);
  // Or encapsulate it as an array 
  print.apply(this, [a, b, c, d]);
  }
  // The following will show " Backlit script "
  example(" back ", " light ", " foot ", " this "); 
  */
  </script>
</head>
<body> 
</body>
</html>

Related articles: