简体   繁体   English

用户从容器中单击时提交一组输入

[英]submit group of inputs when user clicks out of container

I have a form with a wide range of inputs. 我的表单输入范围很广。 Each input is sent to the backend via ajax on change. 每个输入在更改时通过ajax发送到后端。 However, I also have a group of inputs to collect an address as follows; 但是,我还有一组输入来收集地址,如下所示;

<div class="address">
    <input class="addrField" maxsize="100" name="street1" placeholder="street 1" size="40" type="text">
    <input class="addrField" maxsize="100" name="street2" placeholder="street 2" size="40" type="text">
    <input class="addrField" maxsize="100" name="city" placeholder="city" size="40" type="text">
    <input class="addrField" maxsize="100" name="postcode" placeholder="postcode" size="40" type="text">
    <input class="addrField" maxsize="100" name="country" placeholder="country" size="40" type="text">
</div>

i'm wondering if there is an efficient way to allow the user to edit the address fields and then submit all address inputs, as a group, when the user clicks out of the containing div? 我想知道是否有一种有效的方法,当用户单击包含的div时,是否允许用户编辑地址字段,然后作为一个组提交所有地址输入?

I've tried using the change event eg 我尝试使用更改事件,例如

$(".address").change(function() { 
    var $inputs = $(this).children('input');
    submitInput($inputs);
});

however, the change event is fired each time the user changes input fields so I get a number of calls to the backend rather than one. 但是,每次用户更改输入字段时都会触发change事件,因此我得到了多个后端调用,而不是一个。

Is there a way to only call submitInput, when the user clicks out of the containing div? 当用户从包含div的目录中单击时,是否可以仅调用SubmitInput?

FYI, submitInput is as follows: 仅供参考,submitInput如下:

function submitInput(group) {
    $.ajax({
      type: 'post',
      url: '/urladdress',
      data: group.serializeArray()
    });
};

NB. NB。 Putting a click event on the document is problematic because it's inefficient but also because it submits the form for every click outside the doc. 在文档上放置click事件是有问题的,因为它效率低下,而且还因为它会为文档外部的每次单击提交表单。 For my purposes, the form should be submitted once when they have changed the form and then clicked to another part of the page (but not to another address field). 出于我的目的,更改表单后应单击一次表单,然后单击该页面的另一部分(但不要单击另一地址字段)。

As per your requirement, you want to fire the submitInput event only when user click outside of the container div. 根据您的要求,您只想在用户在容器div之外单击时才触发SubmitInput事件。 Here is a code which can help you: 这是可以帮助您的代码:

$(document).mouseup(function (e)
{
var container = $(".address");

if (!$("a").is(e.target) // if the target of the click isn't a link ...
    && !container.is(e.target) // ... or the container ...
    && container.has(e.target).length === 0) // ... or a descendant of the container
{
     var $inputs = $(this).children('input');
     submitInput($inputs);
}
});

Bind the onclick event to the container . 将onclick事件绑定到container Filter events coming from the form-container and the input elements. 过滤来自form-containerinput元素的事件。 Submit the form when you get the event from the outer container. 从外部容器获取事件时,提交表单。

 function submit(){ alert("Submitting."); } $("#viewport").click(function(event){ if(event.target.id !== "form-container" && event.target.nodeName !== "INPUT"){ submit(); } }) 
 #container{ padding:10px; background-color:blue; } #form-container{ margin:10px; padding:10px; background-color:red; } input{ margin:10px; } #viewport{ position:absolute; top:0; left:0; width:100%; height:100%; background-color:rgba(0,0,0,0.5); } 
 <div id="viewport"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <div id="form-container"> <input type="text"> <input type="text"> <input type="text"> <input type="text"> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户点击输入字段时,我正在尝试禁用表单提交按钮 - I'm trying to make the form submit button be disabled when the user clicks out of the input field 用户点击它时隐藏(或禁用)提交按钮 - Hide (or disable) submit button when user clicks on it 如何显示确认提示,显示当用户在文本字段中输入信息并单击js中的“提交”时输入的内容 - How to display an acknowledgement prompt showing what has been entered when user inputs info into text fields and clicks submit in js 如果用户在未填写表格的情况下单击“提交”,则所有按钮均被禁用 - Buttons all disabled if user clicks “submit” without filling out form Rails-用户单击提交按钮时如何更新表中的数据 - Rails - How update data in table when user clicks on submit button 希望在用户单击提交时将javascript用于多个选择菜单 - Looking to use javascript for multiple select menus when user clicks submit 是的,当用户单击提交按钮时,不会验证复选框 - Yup is not validating a checkbox when the user clicks the submit button 当用户单击提交时,我的 AJAX 没有发布或响应 - My AJAX isn't posting or responding when the user clicks submit 用户单击提交按钮时如何调用两个不同的网页? - How to call two different webpages when the user clicks submit button? 用户单击浏览器后退按钮时提交表单 - submit form when user clicks browser back button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM