我有两个表格,one
和two
,根据提交的表格来调用相应的函数。
我想知道是否可以将每个选择器作为数组索引、循环或属性之类的东西检索到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>
if (this.className === "one") { getFoo(); } else { getBoo(); }
- Andreasvar formDependendFunctions = { "one": getFoo, "two": getBoo }; if (formDependendFunctions[this.className]) { formDependendFunctions[this.className](this); }
- Andreas