[英]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.这里getMessageText1
是getMessageText1
。
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.有两个函数getMessageText
和getMessageText1
只会在以后造成混淆。 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.