gpt4 book ai didi

Javascript - 调用内部函数时刷新参数值

转载 作者:行者123 更新时间:2023-11-27 22:51:18 24 4
gpt4 key购买 nike

考虑到这部分代码,我需要知道如何在单击按钮时将文本框的文本写入控制台。

<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>

最佳答案

我不明白你的逻辑为什么要这样做,但是你想要什么可以通过在 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>

关于Javascript - 调用内部函数时刷新参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38034425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com