Javascript-调用内部函数时参数的刷新值
考虑到这部分代码,我需要知道如何在单击按钮时,将文本框的文本写入控制台Javascript-调用内部函数时参数的刷新值,javascript,jquery,function,Javascript,Jquery,Function,考虑到这部分代码,我需要知道如何在单击按钮时,将文本框的文本写入控制台 <body> <input type="text" id="ttb_text" /> <script type="text/javascript"> function AppendButton() { var _text = ''; if (!!arguments[0]) { _text = (arguments[0]['text'] || 'no
<body>
<input type="text" id="ttb_text" />
<script type="text/javascript">
function AppendButton() {
var _text = '';
if (!!arguments[0]) {
_text = (arguments[0]['text'] || 'nothing to log');
}
$('body').append('<input type="button" id="btn_button" value="Log Text" />');
$('body').on('click', '#btn_button', function () {
console.log(_text);
});
}
AppendButton({
text: $('#ttb_text').val()
});
</script>
</body>
函数AppendButton(){
var_text='';
如果(!!参数[0]){
_text=(参数[0]['text']| |“无需记录”);
}
$('body')。追加('');
$('body')。在('click','btn_按钮',函数(){
console.log(_text);
});
}
追加按钮({
文本:$('ttb_text').val()
});
我不理解您的逻辑,为什么要这样做,但通过在onclick回调中获取#ttb_text
值可以实现您想要的
<body>
<input type="text" id="ttb_text" />
<script type="text/javascript">
function AppendButton() {
var _text = '';
if (!!arguments[0]) {
_text = (arguments[0]['text'] || 'nothing to log');
}
$('body').append('<input type="button" id="btn_button" value="Log Text" />');
$('body').on('click', '#btn_button', function () {
_text = $('#ttb_text').val();
console.log(_text);
});
}
AppendButton({
text: $('#ttb_text').val()
});
</script>
</body>
函数AppendButton(){
var_text='';
如果(!!参数[0]){
_text=(参数[0]['text']| |“无需记录”);
}
$('body')。追加('');
$('body')。在('click','btn_按钮',函数(){
_text=$('#ttb_text').val();
console.log(_text);
});
}
追加按钮({
文本:$('ttb_text').val()
});
这是一个将一些组件添加到DOM中的函数的简化示例,我这样做是为了便于理解,顺便说一句,它不起作用:)。我需要的是,单击时,函数必须用#ttb_text的当前值更新_text参数。