所以,再次,我是JQuery的新手,但无论出于何种原因,单击“添加场景”按钮时JQuery不会附加表单。它甚至没有涉及#add的功能。我在.on()行上设置了一个断点,当我点击它时似乎没有识别它。
$(document).ready(function ()
{
$('#add').on('submit',function(e)
{
var form = "<div class='form'>\
<h3> Scenario </h3>\
<form id='scenario'>\
<div>Name: <input type='text' name='ScenarioName'> </div>\
<div>Rate of Investment Return (While Working): <input type='number' name='Working'></div>\
<div>Rate of Investment Return (Retired): <input type='number' name='Retired'></div>\
<div>Desired Retirement Yearly Income: <input type='number' name='desiredInc'></div>\
<div><input type='submit' value='Submit'></div>\
</form>\
<div><button id='add' type='submit'>Add Scenario </button></div>\
</div>"
$('body').append($(form));
e.preventDefault();
});
});
<body>
<div class='form'>
<h3> Basic Information </h3>
<form id='basic'>
<div>Year of Birth: <input type='number' name='YOB'> </div>
<div>Current Savings: <input type='number' name='CurrSav'>
</div>
<div>Expected Retirement Age: <input type='number' name='RetAge'></div>
<div>Life expectancy: <input type='number' name='LifeExp'>
</div>
<div><input type='submit' value='Submit'></div>
</form>
</div>
<div class='form'>
<h3> Scenario </h3>
<form id='scenario'>
<div>Name: <input type='text' name='ScenarioName'> </div>
<div>Rate of Investment Return (While Working): <input type='number' name='Working'></div>
<div>Rate of Investment Return (Retired): <input type='number' name='Retired'></div>
<div>Desired Retirement Yearly Income: <input type='number' name='desiredInc'></div>
<div><input type='submit' value='Submit'></div>
</form>
<div><button id='add' type='submit'>Add Scenario </button></div>
</div>
</body>
答案 0 :(得分:3)
而不是submit event处理程序,你需要使用click event处理程序 - 提交事件是表单元素,你正在注册按钮元素的处理程序。
$(document).ready(function () {
$('#add').on('click', function (e) {
var form = "<div class='form'>\
<h3> Scenario </h3>\
<form id='scenario'>\
<div>Name: <input type='text' name='ScenarioName'> </div>\
<div>Rate of Investment Return (While Working): <input type='number' name='Working'></div>\
<div>Rate of Investment Return (Retired): <input type='number' name='Retired'></div>\
<div>Desired Retirement Yearly Income: <input type='number' name='desiredInc'></div>\
<div><input type='submit' value='Submit'></div>\
</form>\
</div>";
$(this).parent().before(form)
e.preventDefault();
});
});
演示:Fiddle
答案 1 :(得分:1)
试试这个:
$('#add').on('click', function(e) // <-- click instead of submit
{
var form = "<div class='form'>\
<h3> Scenario </h3>\
<form id='scenario'>\
<div>Name: <input type='text' name='ScenarioName'> </div>\
<div>Rate of Investment Return (While Working): <input type='number' name='Working'></div>\
<div>Rate of Investment Return (Retired): <input type='number' name='Retired'></div>\
<div>Desired Retirement Yearly Income: <input type='number' name='desiredInc'></div>\
<div><input type='submit' value='Submit'></div>\
</form>\
<div><button id='add' type='submit'>Add Scenario </button></div>\
</div>";
$('body').append(form); // no need to wrapping with jQuery
});
答案 2 :(得分:0)
您的点击事件处理程序错误。
当用户尝试提交表单时,submit
事件将发送到元素。它只能附加到form
元素。
更改此
$('#add').on('submit',function(e)
为:
$('#add').on('click',function(e)