gpt4 book ai didi

javascript - 如何将自定义数据发送到 JQuery 单击事件?

转载 作者:行者123 更新时间:2023-11-29 16:06:53 26 4
gpt4 key购买 nike

我有这个 JQuery 点击事件:

jq("#generateButton").click({key: "hello world"}, function () {
console.dir(this);// this is the "generateButton" html element.
// jq(this) would be the jquery object of the generate button
frnConstr.setDataObject(this);
frnConstr.gameObject.state.start(frnConstr.EnteriorScreen.KEY);
});

我怎么可能将自定义数据发送到此事件,例如打印 {key: "hello world"}?有可能吗?

注意:在我定制的工作环境中,jq() 是 $() 或 jquery()。

注意:jQuery 文档说 eventData 可以是任何东西,这里是官方文档: https://api.jquery.com/click/ :)

最佳答案

通常你会为此使用数据属性并用

抓取它
jq("#generateButton").on("click",function() {
var custom = $(this).data("custom");
});

使用

<button id="generateButton" data-custom="hello world"> 

您指的是 event.data在绑定(bind)时发送

jq("#generateButton").on("click",{"what":"hello"}, function(event) {
var custom = $(this).data("custom");
console.log(event.data.what+" "+custom); // hello world
});

<button id="generateButton" data-custom="world">

例子

$(function() {
$("#generateButton1").on("click", function() {
var custom = $(this).data("custom");
console.log(custom);
});


$("#generateButton2").on("click", {
what: "goodbye"
}, function(event) { // needed here
var custom = $(this).data("custom");
console.log(event.data.what + " " + custom); // hello world
});

$("#generateButton3").on("click", function() {
var formdata = $(this).closest("form").serialize();
console.log(formdata);
});


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button type="button" id="generateButton1" data-custom="hello world">Hello world</button>

<button type="button" id="generateButton2" data-custom="world">Goodbye world</button>
<hr/>
Serializing:
<form>
<input type="text" name="field1" value="f1" /><br/>
<input type="text" name="field2" value="f2" /><br/>
<button type="button" id="generateButton3">Click to serialize</button>
</form>

关于javascript - 如何将自定义数据发送到 JQuery 单击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39294473/

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