简体   繁体   English

jQuery:我可以从非表单输入中提交值吗?

[英]JQuery: can I submit values from inputs that aren't in a form?

Simple as that: I have some inputs that are rendered inside another form element, but I want to submit then individually. 就这么简单:我有一些输入呈现在另一个表单元素中,但是我想分别提交。

I tried "serializing" the div that contains them, but it didn't work. 我尝试“序列化”包含它们的div,但是没有用。 Is there an easy way to do this? 是否有捷径可寻?

My code: 我的代码:

var form = $("#my_div");
var get_data = $(form).serialize();  /* Get #my_div inputs value */
$.get(self.url_form_rendering, get_data,
function (data){
    /* handle server response */
})

Serialize returns "", any ideas that don't involve redesigning the layout to avoid form nesting? 序列化返回“”,是否有不涉及重新设计布局以避免表单嵌套的想法?

Do it by building get_data as an object: 通过将get_data构建为一个对象来实现:

var get_data = {
    field1: $("#field1").val(),
    field2: $("#field2").val(),
    ...
};

You can use the above for just the extra elements. 您可以将以上内容仅用于其他元素。 For the form, you can add them with a loop: 对于表单,可以添加一个循环:

$("#formid :input").each(function() {
    get_data[this.name] = this.value;
});

You could also give the elements outside the form a class, and select them and iterate similarly. 您还可以给表单外部的元素一个类,然后选择它们并类似地进行迭代。

进行序列化时,请确保输入具有自己的ID,然后尝试以下代码:

$('#my_div *').serialize();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM