考虑到这部分代码,我需要知道如何在单击按钮时将文本框的文本写入控制台。
<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/