简体   繁体   English

如何将变量从 HTML 页面传递给子函数

[英]How to pass variable to child function from HTML page

I want to pass a value from HTML page to child function from parent function.我想将一个值从 HTML 页面传递给父函数的子函数。

HTML Page: HTML页面:

<div class="bottom_wrapper clearfix">

  <div class="message_input_wrapper">
    <input class="message_input" placeholder="Type your message here..." />
  </div>

  <div class="send_message">
    <div class="icon"></div>
    <div class="text">Send/div>
  </div>
</div>

Parent Function Call:父函数调用:

$('.send_message').click(function (e) {
  return [sendMessage(getMessageText()),sendMessage1(getMessageText1())];
});

$('.message_input').keyup(function (e) {
  if (e.which === 13) {
    return [sendMessage(getMessageText()),sendMessage1(getMessageText1())];
  }
});

here getMessageText1 is child function.这里getMessageText1getMessageText1

Child Function:子函数:

getMessageText1 = function () {
  var result="";
  var id = Parent_FUNC_INPUT;
  $.ajax({
    url:"func.php",
    type: 'POST',
    data: ({id:id}),
    async: false,  
    success:function(data) {
      result = data; 
    }
  });

I want to populate [[id]] variable in child function from parent function.我想从父函数填充子函数中的 [[id]] 变量。

First, I'll do my best to clean up the HTML:首先,我会尽力清理 HTML:

<div class="bottom_wrapper clearfix">
    <div class="message_input_wrapper">
        <input class="message_input" placeholder="Type your message here..." />
    </div>
    <div class="send_message">
        <div class="icon"></div>
    </div>
    <div class="text">Send</div>
</div>

Using proper indentation will make things far easier to read.使用适当的缩进将使事情更容易阅读。 And while we're on the subject, you may want to use dashes - instead of underscores _ in your class names, as that's the common convention.当我们讨论这个主题时,您可能希望在类名中使用破折号-而不是下划线_ ,因为这是常见的约定。

On to your question, it seems like what you want to do is simply pass an argument to getMessageText1 from within (as you refer to it) a "parent" function bound to an event listener.关于您的问题,您似乎想要做的只是从绑定到事件侦听器的“父”函数内部(如您所指)将参数传递给getMessageText1

So you'd define this "child" function with a single parameter:所以你可以用一个参数定义这个“子”函数:

function getMessageText1(Parent_FUNC_INPUT) {
    ...
    var id = Parent_FUNC_INPUT;
    ...
}

And then you can just call it with getMessageText1(value) where value is whatever you want to pass in.然后您可以使用getMessageText1(value)调用它,其中 value 是您想要传入的任何内容。

One more note: for readability's sake I recommend you do not name your functions the way you have.还有一点要注意:为了可读性起见,我建议您不要按照您的方式命名您的函数。 Having two functions getMessageText and getMessageText1 will just be a source of confusion later on.有两个函数getMessageTextgetMessageText1只会在以后造成混淆。 Instead, think of something more descriptive, ala getMessageTextFromID .相反,想想更具描述性的东西,ala getMessageTextFromID

Hopefully I answered the question you meant to ask.希望我回答了你想问的问题。 Let me know.让我知道。

暂无
暂无

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

相关问题 如何将django变量从HTML页面传递到javascript - How to pass a django variable from html page to javascript 如何将动态变量从一个 html 页面传递给 javascript? - How to pass a dynamic variable from one html page to javascript? 如何将变量从 javascript 函数传递到 php 文件而不重新加载编写 javascript 函数的当前 html 页面? - how to pass a variable from javascript function to php file without reloading the current html page in which javascript function is written? 如何将 PHP 变量从 Blade 格式的 HTML 传递给 JavaScript 函数 - How to pass PHP variable to JavaScript function from Blade format HTML 如何从 doPost function 获取变量以传递给 Html 模板 - How to get variable from doPost function to pass to Html Template 如何将变量从我的控制器传递到 HTML,然后传递到 JS 函数? - How to pass a variable from my controller, to the HTML, then to a JS function? 如何将javascript / jquery函数从一个HTML页面传递到另一HTML页面 - How to pass a javascript/jquery function from one html page to another 如何将 javascript 变量值从一个 html 页面传递到另一个 html 页面? - How to pass the javascript variable value from one html page to the another html page? 如何将变量传递给 HTML 中的 JavaScript 函数? - How to pass variable to the JavaScript function within HTML? Angular Ionic 4 如何将变量从 page.html 传递到 page.ts - Angular Ionic 4 How to pass a Variable from page.html to page.ts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM