如何将参数传递给 JQuery 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13392476/
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 pass arguments into JQuery function?
提问by sjkon
I want to pass a value to a Jquery function. Below is the Javascript sample code. I want to convert this javascript function to a jquery function. How can I pass the state name into a jquery onclick function?
我想将一个值传递给 Jquery 函数。下面是 Javascript 示例代码。我想将此 javascript 函数转换为 jquery 函数。如何将状态名称传递给 jquery onclick 函数?
<a onclick="showState('state_name')">ADD STATE </a>
function showState(state_name){
openbox_state(state_name);
}
please help me.
请帮我。
回答by nnnnnn
You don't really make it clear whether you have a series of anchors to add different states, or...?
你并没有真正说清楚你是否有一系列锚点来添加不同的状态,或者......?
If you do, you can do something like this to record the appropriate state name for each link:
如果这样做,您可以执行以下操作来记录每个链接的适当状态名称:
<a data-stateName="state_name">ADD STATE </a>
And then in JS, with jQuery:
然后在 JS 中,使用 jQuery:
$(document).ready(function() {
$("a[data-stateName]").click(function() {
openbox_state( $(this).attr("data-stateName") );
});
});
This removes the inline JavaScript from your html, and then uses jQuery to bind a click handler only to those <a>
elements that have a data-stateName
attribute. The handler then gets the value of that attribute to pass to the openbox_state()
function.
这将从您的 html 中删除内联 JavaScript,然后使用 jQuery 将单击处理程序仅绑定到<a>
具有data-stateName
属性的那些元素。然后处理程序获取该属性的值以传递给openbox_state()
函数。
The document ready handler ensures that the anchor click binding doesn't happen until the document actually is ready, i.e., after the elements have been parsed and can be accessed from JS. (You don't need a ready handler if you put your JS in a script block at the end of the body element.)
文档就绪处理程序确保在文档实际准备好之前不会发生锚点单击绑定,即在元素被解析并且可以从 JS 访问之后。(如果将 JS 放在 body 元素末尾的脚本块中,则不需要准备好的处理程序。)
回答by frenchie
Like this:
像这样:
$('#MyID').click(function () {
var StateName = $(this).text();
showState(StateName);
});
And then for the HTML, you could put some ID like this:
然后对于 HTML,你可以像这样输入一些 ID:
<a id="MyID">ADD STATE </a>
Or, if you have many states (which I assume is what you're trying to do) then you could reference the link by CSS class like this:
或者,如果您有很多状态(我认为这就是您要执行的操作),那么您可以通过 CSS 类来引用链接,如下所示:
<a class="StateSelector">ADD STATE </a>
And then your jquery code becomes:
然后你的 jquery 代码变成:
$('.StateSelector').click(function () {
var StateName = $(this).text();
showState(StateName);
});
To wire up the event handling, you can put the jquery code in the document ready function like this:
要连接事件处理,您可以将 jquery 代码放在文档就绪函数中,如下所示:
$(document).ready(function() {
$('.StateSelector').click(function () {
var StateName = $(this).text();
showState(StateName);
});
});
回答by Jesse
You can do something like this:
你可以这样做:
<a onclick="showState()" id="florida">ADD STATE </a>
function showState(state_name){
var state_name = $(this).attr('id')
openbox_state(state_name);
}
This function will retrieve the "id" value from the link. You can then pass the value onto your "openbox_state()" function. :-)
此函数将从链接中检索“id”值。然后,您可以将该值传递给您的“openbox_state()”函数。:-)
回答by yogi
If you have multiple a
with different arguments like this.
如果你有多个a
像这样的不同参数。
<a onclick="showState('Delhi')">ADD STATE</a>
<a onclick="showState('Punjab')">ADD STATE</a>
<a onclick="showState('Gujrat')">ADD STATE</a>
function showState(state_name){
openbox_state(state_name);
}
then you can use custom attributes like this
那么你可以使用这样的自定义属性
<a state="Delhi">ADD STATE</a>
<a state="Punjab">ADD STATE</a>
<a state="Gujrat">ADD STATE</a>
$('a').click(function () {
var StateName = $(this).attr("state");
openbox_state(StateName);
});
回答by Sushanth --
<a>Montana</a>
$('a').on('click', function(){
var state = this.text ; // OR $(this).text(); OR $(this).html();
showState(state);
});