0

因此,再次,我是 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>
4

3 回答 3

3

您需要使用click 事件处理程序而不是提交事件处理程序 - 提交事件是针对表单元素的,您正在为按钮元素注册处理程序。

$(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();
    });
});

演示:

$(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();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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>

于 2013-10-02T09:05:05.987 回答
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
    });
于 2013-10-02T09:11:19.983 回答
0

您的点击事件处理程序是错误的。

submit当用户尝试提交表单时,该事件被发送到元素。它只能附加到form元素上。

改变这个

 $('#add').on('submit',function(e)

至:

$('#add').on('click',function(e)
于 2013-10-02T09:07:39.120 回答