[英]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-container
和input
元素的事件。 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.