我很新的JavaScript和jQuery,并遇到了一个问题,我还没有找到解决方案。我会尝试以相当简单的方式绘制它。如何克服函数中的嵌套?
我有一个页面为用户提供了一些选择(它的游戏,所以..)如攻击或防守,使用什么武器。然后它加载两个html,每个都有一个“提交”按钮。提交按钮绑定到(分开的)将数据传递给ajax的函数。
我遇到的问题是我埋在嵌套函数中。那有意义吗?我的代码看起来是这样的:
代码:
$(function() {
$('#choice1').click(function() {
//do some stuff
});
$('#choice2').click(function() {
//do some stuff
});
$('#choice3').click(function() {
//do some stuff, including creating the choices below, and their submit boxes
$('#subChoice1').click(function() {
//send data with ajax to a php document. get result
//create some text on my document to display results
//create an input button, along with an id="subButton1"
});
$('#subChoice2').click(function() {
//send data with ajax to a php document. get result
//create some text on my document to display results
//create an input button, along with id="subButton1"
//(yes, feed both back to same func)
});
}); // end choice3
$('#subButton1').click(function() {
//my buttons in the subChoices do not call this function(!!??)
});
});
编辑:链接不再起作用,对不起。而且我创建了living example here.
感谢您的任何提示或指示。我几乎可以肯定,只是有一件简单的事情,我错过了或没有意识到这会让我走上正轨。
为什么你有你的点击绑定嵌套? – Headshota 2011-04-15 21:22:11
,因为我不知道更好的方法来做到这一点。我正在学习。 – jeremy 2011-04-15 21:37:59