我有两个表单一和二,根据提交的表单,我想在逻辑结束时根据提交的值调用一个函数.
我想知道我是否可以将每个选择器作为数组索引,循环或属性或其他内容检索到var = tagsOrAnswerers?然后也许我可以将该参数传递给我将被调用的函数.
<html>
<form class="one">
<input type="text" name="foo">
<input type="submit" value="Submit">
</form>
<form class="two">
<input type="text"name="boo">
<input type="submit" value="Submit">
</form>
</html>
<script>
$('.one,.two').submit( function(){
var fooOrBoo = $(this).find("input[name='foo'],[name='boo']").val();
getBoo(ifBoo); //run this function if var fooOrBoo = 'boo';
getFoo(ifFoo); //run this function if var fooOrBoo = 'foo';
});
</script>
最佳答案 为每个表单的答案指定一个通用名称.我把它命名为“答案”
<div class="forms">
<form class="one">
<input type="text" name="answer">
<input type="submit" value="Submit">
</form>
<form class="two">
<input type="text"name="answer">
<input type="submit" value="Submit">
</form>
</div>
这是脚本:
<script>
$('.forms > form').submit( function(){
var data = $( this ).serializeArray();
console.log(data); // gives the current data of the forms
//using the data you can call whatever function you want using a switch statment
});
</script>