简体   繁体   English

如何将值从Visualforce组件中的脚本传递到其控制器

[英]How to pass values from Script in visualforce component to its controller

I am having a visualforce component with some script written in it and I directly want to pass some values to the controller.this is My javascript code. 我有一个visualforce组件,其中写有一些脚本,我直接想将一些值传递给controller.this是我的javascript代码。

<script>
function uploadComplete(evt) {
  var city = 'Shimla';
  var location = 'kkllkk'
  **i want to pass city and location in IWantToDebug method**
  function IWantToDebug() {

         MyController.IWantToDebug(city,location, function(result, event) {
    // Set the inputField value to the result in here
   });
} </script>

My apex controller class method is like. 我的apex控制器类方法就像。

 public void IWantToDebug(String city , String location) {

      System.debug('======================= ' + data);

 }

You can use actionfunction to pass the values to controller 您可以使用动作函数将值传递给控制器

add following to your html part of component: 将以下内容添加到组件的html部分:

<apex:actionFunction action="{!IWantToDebug}" name="IWantToDebugJavascriptSide" rerender="someComponentIdToRender">
    <apex:param name="city " value=""/>
    <apex:param name="location" value=""/>
</apex:actionFunction>

change your javascript to something like 将你的javascript改成类似的东西

    <script>
       function uploadComplete(evt) {
          var city = 'Shimla';
          var location = 'kkllkk'
          **i want to pass city and location in IWantToDebug method**
          function IWantToDebug() {
          IWantToDebugJavascriptSide(city,location);
      });
   </script>

And change your Controller to something like 并将您的控制器更改为类似的

public PageReference IWantToDebug() {
     String city , String location;
     if (Apexpages.currentPage().getParameters().containsKey('city')){
       city = Apexpages.currentPage().getParameters().get('city'));
     }
     if (Apexpages.currentPage().getParameters().containsKey('location')){
        location= Apexpages.currentPage().getParameters().get('location'));
     }

     System.debug('======================= ' + city + ' ' +location);
     return null;
  }

For more reference on how to use actionfunction please visit https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm 有关如何使用动作功能的更多参考,请访问https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm

Thank you 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM