gpt4 book ai didi

javascript - 如何使某个 javascript 函数在页面加载时执行其所有元素?

转载 作者:行者123 更新时间:2023-11-28 21:08:38 27 4
gpt4 key购买 nike

我有一个 JavaScript 函数定义如下(请注意,它不使用 jquery):

function getCalculationFormsByType(selectObject, parentNode, countIndex)
{
var operationID = parseInt(selectObject.value, 10);

var divs = parentNode.getElementsByTagName("DIV");

// the rest of the function goes here, it isn't really important ...
}

该函数按以下方式执行(同样,没有 jquery):

<select name="operationChoose[]" onchange="getCalculationFormsByType(this, this.parentNode.parentNode, '1')" >

到目前为止一切正常。问题是我需要在页面加载时为页面上的所有 select 元素执行此函数。像这样(我的想法使用jquery,但这对于解决方案来说不是必需的):

$("document").ready(function(){
$("select[name='operationChoose[]']").each(function(){
getCalculationFormsByType(---I DO NOT KNOW WHAT TO PASS HERE---);
});
});

如您所见,我的问题是我不知道将什么传递到 jQuery 中的函数中。我不知道 javascript 中的这 3 个值是什么以及如何在 jQuery 的 each 循环中获取它们。

最佳答案

应删除 $("document").ready 中的引号。另外,$(..function here..)$(document).ready(...) 的简写。

这是正确的实现:

$(function() {
$("select[name='operationChoose[]']").each(function(i) { // <-- i-th element
// this points to the <select> element, HTMLSelectElement
getCalculationFormsByType(this, this.parentNode.parentNode, i);
});
});

关于javascript - 如何使某个 javascript 函数在页面加载时执行其所有元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9250830/

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