gpt4 book ai didi

javascript - 为每个表单输入调用 jQuery 函数

转载 作者:行者123 更新时间:2023-12-02 17:01:10 25 4
gpt4 key购买 nike

我目前正在使用 http://jsfiddle.net/NvynC/663/ 中找到的代码除了一件事之外,它的效果很好......

此代码触发 fiddle 中显示的resizeInput()函数...

$('input[type="text"]')
// event handler
.keyup(resizeInput)
// resize on page load
.each(resizeInput);

它会在页面首次加载时触发它,并在每次“keyup”时再次加载,但我添加了一些额外的代码,这些代码根据其他字段中更改的值更新各种文本字段。我对在需要时在给定时间调整所有字段大小的代码是什么感到困惑。

下面的代码不是我正在使用的代码,而是简单的事情,如果你能告诉我如果我以以下方式调用它会使用什么代码,我将能够弄清楚如何将其放入我的实际代码....

function resize_fields()
{
// WHAT CODE GOES HERE ????
}
resize_fields();
<小时/>

更新以使您更好地了解我在做什么......

这是我使用的代码...以我的形式...

<select name="user" id="user" onchange=update_user_fields(this.value)>
<option></option>

脚本...

<script>
function update_user_fields(id)
{
user[id]();
}

user[<?php echo $user->ID; ?>] = function()
{
// I do a bunch of field value changes here and they work perfectly
// ///////
// What I do not know is how to call the resizeInput function referred to in the fiddle above
// ///////
}


</script>
<小时/>

我尝试将以下代码放在上面缺少代码的部分中,但它不起作用!

jQuery(function($) {
$('input[type="text"]')
// resize all
.each(resizeInput);
});
<小时/>

原来我的头骨很厚...下面的答案毕竟是正确的..这是工作代码...

<select name="user" id="user" onchange=update_user_fields(this.value)>
<option></option>

脚本...

<script>
function update_user_fields(id)
{
user[id]();
}

user[<?php echo $user->ID; ?>] = function()
{
// I do a bunch of field value changes here and they work perfectly

// ///////
jQuery(function($) {
$('input[type="text"]').keyup();
});
// ///////

}


</script>

最佳答案

不是 100% 清楚,但似乎您希望能够以编程方式更改值并同时调用调整大小方法。

要执行此操作,您只需在更改值后触发使用处理程序的事件。

示例:

$('#someInput').val('some new string').keyup();

或者一次性完成所有这些:

$('input[type="text"]').keyup();

这与执行相同:

$('input[type="text"]').trigger('keyup');

关于javascript - 为每个表单输入调用 jQuery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25705409/

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