【问题标题】:How do I collapse this form when clicking the ".submit" button?单击“.submit”按钮时如何折叠此表单?
【发布时间】:2012-11-30 12:05:46
【问题描述】:

我有一个可折叠的(输入您的信用卡信息)表格。当我单击它时,它会打开,但是当我单击提交时,我希望它关闭。这是我的html:

<div class= "addStuff">
<div data-role="collapsible" data-inset="true" id="ccdiv">
   <h5> New credit card</h5>         
      <div data-role="fieldcontain">
            <form id = "myForm">
        <input type="password" name="credit_card_number" id="credit_card_number" value="" placeholder="Credit Card Number">
        <input type="password" name="security_code" id="security_code" value="" placeholder="Security Code">
        <input type="date" name="expiration_date" id="expiration_date" value="" placeholder="Expiration Date">
        <input type="password" name="name" id="name" value="" placeholder="Name On Card">
        <input type="password" name="street_address" id="street_address" value="" placeholder="Street Address">
        <input type="text" name="city" id="city" value="" placeholder="City">
        <input type="number" name="zip_code" id="zip_code" value="" placeholder="Zip Code">

            <button type="button" class="submit" name="submit" value="submit">Submit</button>
            </form>
      </div>
</div>

这是我的 JS 尝试:

$(document).ready(function() {
$('.submit').click(function(){
    var creditcard = $('#credit_card_number').val();
    $('.accounts #CreditCards div[data-role="content"] #swipeMe').append('<li data-swipeurl="#" ><a href="#">' + creditcard + '</a></li>');
   // refresh view 
   $('#CreditCards ul').listview('refresh');

   // hide form
 alert('test'); 


 $('#myForm').slideUp();
 $('#myForm').trigger('collapse');

    });
});

【问题讨论】:

  • 附加触发事件提交,连同点击
  • 为什么您的输入不在表单中?

标签: jquery html jquery-mobile collapsable


【解决方案1】:

这是一个 JsFiddle 的链接,我试图让您的代码正常工作 http://jsfiddle.net/rvqRX/

我逐行逐行注释,一次注释掉一个,发现我必须注释掉它才能正常运行的唯一行是这个

$('#CreditCards ul').listview('refresh');

不知道为什么……这对你有意义吗?

【讨论】:

  • 这对我来说毫无意义,但它完全有效。必须是 jQuery Mobile 之类的东西,但你很棒。
【解决方案2】:

在表单的 submit() 中,确保使用 JQuery 方法调用折叠表单的触发器。那是你要找的吗?

【讨论】:

【解决方案3】:

ID 添加到您的表单&lt;form id="myForm"&gt;...&lt;/form&gt;

使用JQuery animation 将表单折叠到您想要的位置:

$('#myForm').slideUp();

【讨论】:

  • 我把所有的东西都放在一个表格里并试过了,但还是没有。您可以在上面的示例中看到我的编辑。我究竟做错了什么?谢谢:)
  • 只需将id添加到表单中
  • 甚至 $('#myForm').hide(); ?
  • 还是什么都没有。会不会是 jQueryMobile 的东西?
  • 可以!按照设计,您加载的页面的 JS 代码不会被执行。仅考虑加载的第一个文件的 JS 代码。尝试在您的点击事件中调用“alert('test')”。如果未显示警报消息,则不会执行您的代码。如果是这种情况,我将使用解决方法更新我的答案
猜你喜欢
  • 2023-03-05
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 2019-08-26
相关资源
最近更新 更多