如何在 jQuery 中调用 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41813339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call javascript function inside jQuery
提问by Harry Mac
We have this tag with a javascript function in our HTML,
我们的 HTML 中有这个带有 javascript 函数的标签,
<select name="My_Saved_Billing" onchange="Choose_My_Saved_Billing(this.selectedIndex)" >
<option>Select</option>
<option value="1714">Address line 1, QC</option>
</select>
<script type="text/javascript">
function Choose_My_Saved_Billing(arg_index) {
switch(arg_index) {
// some commands here
}
}
</script>
And I also added a jQuery to it which is below so that on windows load, it will automatically select the second option.
我还在下面添加了一个 jQuery,以便在 Windows 加载时,它会自动选择第二个选项。
<script type="text/javascript">
$(window).load(function(){
$("select").val($("select option:eq(1)").val());
});
</script>
But is it possible to call javascript function using jQuery? If so, how should I call this one?
但是可以使用 jQuery 调用 javascript 函数吗?如果是这样,我该如何称呼它?
Should I use Choose_My_Saved_Billing(this.selectedIndex)or Choose_My_Saved_Billing(arg_index)or you might know something. I've tried these two but none are working. Please let me know. Just a beginner here.
我应该使用Choose_My_Saved_Billing(this.selectedIndex)或Choose_My_Saved_Billing(arg_index)或者你可能知道一些东西。我试过这两个,但没有一个工作。请告诉我。这里只是初学者。
回答by kinakuta
Yes, it's possible to call functions inside a jQuery ready block. Since you've defined the function at global scope (should probably move this into the jQuery ready block or, if you want to go to the trouble, into a module), it can be called from anywhere. So inside your ready block:
是的,可以在 jQuery 就绪块中调用函数。由于您已经在全局范围内定义了该函数(可能应该将其移动到 jQuery 就绪块中,或者,如果您想麻烦的话,可以将其移动到模块中),因此可以从任何地方调用它。所以在你准备好的块中:
$(function () {
// do stuff
Choose_My_Saved_Billing(args);
});
回答by metame
jQuery is JavaScript. It's just a library for JavaScript. The main jQuery global $is a JavaScript function that takes a valid selector as an argument and provides several methods on the return value of that function.
jQuery 是 JavaScript。它只是一个 JavaScript 库。主要的 jQuery 全局$是一个 JavaScript 函数,它接受一个有效的选择器作为参数,并提供了几个关于该函数返回值的方法。
So calling a JavaScript function inside the callback function to .loadis not an issue.
所以在回调函数中调用 JavaScript 函数.load不是问题。
It is not clear what the Choose_My_Saved_Billingfunction actually does.
目前尚不清楚该Choose_My_Saved_Billing功能实际上做了什么。
回答by Cruiser
Think about what's happening here. In your onchange event you're calling the function with the index of the selected option passed as an argument. Since JQuery is just a library of shortcuts for things you can do in JavaScript, we should easily be able to do the same thing.
想想这里发生了什么。在您的 onchange 事件中,您使用作为参数传递的所选选项的索引来调用函数。由于 JQuery 只是您可以在 JavaScript 中执行的操作的快捷方式库,因此我们应该能够轻松地执行相同的操作。
So let's get the element for which we want the selected index:
因此,让我们获取我们想要为其选择索引的元素:
// maybe think about adding an ID here for better selection
var select = $('select[name^="My_Saved_"]');
Then let's get the index with a change event, then call the function:
然后让我们用change事件获取索引,然后调用函数:
var index = 0;
select.change(function(){
index = select.selectedIndex || 2; // set the index to default to 2
Choose_My_Saved_billing(index);
});
回答by Matt Mokary
Instead of using onchange="...", just use jQuery to attach a change listener:
而不是使用onchange="...",只需使用 jQuery 附加更改侦听器:
$(window).load(function() {
$('.colors_backgroundneutral select').on('change', function () {
Choose_My_Saved_Billing(this.value);
});
});
回答by Pyrewolf
As jQuery is a more simple and advanced JavaScript solution, my guessing is you can call you JS function like this:
由于 jQuery 是一种更简单和高级的 JavaScript 解决方案,我猜你可以像这样调用你的 JS 函数:
$(window).load(function(){
my_js_function(arg1, arg2);
});
Now, what you want is to call the JS function named Choose_My_Saved_Billing()with argument arg_index
现在,您想要的是调用Choose_My_Saved_Billing()以参数命名的 JS 函数arg_index
So, your jQuery will look like this:
因此,您的 jQuery 将如下所示:
$(window).load(function(){
Choose_My_Saved_Billing(arg_index);
});
This only works if the function is already declared through raw code, on via the <script type="text/javascript" src="path/to/my_file.js">head tag.
这只适用于已经通过原始代码声明的函数,通过<script type="text/javascript" src="path/to/my_file.js">head 标签。
It should work like a charm, if not, feel free to share the errors returned by your browser.
它应该像魅力一样工作,如果没有,请随时分享浏览器返回的错误。

