gpt4 book ai didi

javascript - 如何获取通配符表单 ID

转载 作者:行者123 更新时间:2023-11-30 13:52:08 25 4
gpt4 key购买 nike

如何归档以获得通配符表单名称或 ID?

我需要获取表单 ID 并且两个表单是相同的我只想要表单 ID 中的最后一位数字例如在 form1 中我需要 1 请指教

<script>
$(document).on('submit', '[id^=addformj]', function(event){
event.preventDefault();

var formid=//I need to get form id here

alert('You have submited form ' + formid);


}
</script>

HTML是这样的

<form id="form1">
<input id="value1">
</form>

<form id="form2">
<input id="value2">
</form>

如果提交了表单 ID 1,我想提醒 1;如果提交了表单 ID 2,我想提醒 2

<script>
$(document).on('submit', '[id^=addformj]', function(event){
event.preventDefault();

var formid=//I need to get form id here

alert('You have submited form ' + formid);


}
</script>

最佳答案

可以通过this 关键字访问表单元素。这是一个例子:

$(document).on('submit', '[id^=form]', function(event) {
event.preventDefault();

console.log('The form: ', this);

var formid = this.id;

// Change substring start value based on length of the formid prefix.
alert('You have submited form ' + formid.substr(4));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form id="form1">
<input id="value1">
<input type="submit">
</form>

<form id="form2">
<input id="value2">
<input type="submit">
</form>

关于javascript - 如何获取通配符表单 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58030770/

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