简体   繁体   English

将选定的选择选项添加到jQuery表单提交

[英]Add selected select option to jquery form submit

Just having a little trouble with part of my code. 在我的部分代码上遇到了一些麻烦。

Basically new elements are added into the page through .html() and ajax response. 基本上,新元素通过.html()和ajax响应添加到页面中。 An example of added elements are commented in the code. 在代码中注释了添加元素的示例。 Because the codes were added into the page through .html the form wasn't submitting the added fields. 由于代码是通过.html添加到页面中的,因此表单未提交添加的字段。 So I used .append($('.newfields') which works perfectly on the checkbox but fails on the select box which is the purpose of this question. 因此,我使用了.append($('。newfields'),它在复选框上非常有效,但在选择框上却失败,这是此问题的目的。

Any way to include the new select boxes on form submit? 有什么办法可以在表单提交中包括新的选择框?

Thank you all in advance for you help! 预先感谢大家的帮助!

 $('.button').click(function(){ $('#domaincheckform').append($('.newfields')); $( "#domaincheckform" ).submit(); }); 
 <form action="cart.php" id="domaincheckform" method="post"> <select form="domaincheckform" name="termselect[]"> <option value="1">1 year</option> <option value="2">2 year</option> </select> <input type="checkbox" name="did[]" value="example1"> <!--EXAMPLE ADDED FIELD--> <select form="domaincheckform" class="newfields" name="termselect[]"> <option value="1">1 year</option> <option value="2">2 year</option> </select> <input type="checkbox" class="newfields" name="did[]" value="example2"> <!--END OF EXAMPLE ADDED FIELD--> <span class="button">Button</span> </form> 

You need to wrap your select inside a div 您需要将select包装在div中

try this 尝试这个

 <form action="cart.php" id="domaincheckform" method="post">

<select form="domaincheckform" name="termselect[]">
<option value="1">1 year</option>
<option value="2">2 year</option>
</select> 
<input type="checkbox" name="did[]" value="example1">

<!--EXAMPLE ADDED FIELD-->  
<div class="append_content"><select form="domaincheckform" class="newfields" name="termselect[]">
    <option value="1">1 year</option>
    <option value="2">2 year</option>
    </select> </div>
<input type="checkbox" class="newfields" name="did[]" value="example2">
<!--END OF EXAMPLE ADDED FIELD-->  

<span class="button">Button</span>
</form>

Also make changes in your js like this 还可以像这样在您的js中进行更改

<script>
   $(document).ready(function() {
        $('.button').click(function(){
            $('#domaincheckform').append($('.append_content').html());
            $( "#domaincheckform" ).submit();
        });
   });  
</script>

now the select is inside a div append_content its easy to fetch its content as select has multiple html content so it will return object if you use only selector name $(".newfields") 现在,选择位于div append_content内,因为选择具有多个html内容,因此易于获取其内容,因此,如果仅使用选择器名称$(".newfields") ,它将返回对象

$(selector).click(function(){}) sometimes fails to work. $(selector).click(function(){})有时无法正常工作。 Try using $(selector).on("click", function(){}) 尝试使用$(selector).on("click", function(){})

It worked here: https://jsbin.com/vemuxukehi/edit?html,js,output 它在这里工作: https//jsbin.com/vemuxukehi/edit?html,js,输出

Heres is a simple working example that adds the and displays it in the submitted form data 这是一个简单的工作示例,它添加并在提交的表单数据中显示

https://jsfiddle.net/uapejgu5/ https://jsfiddle.net/uapejgu5/

$(function () {
    $("#submit").click(function () {
        $("#result").html($('#myform').serialize())
        return false
    })
    $("#add").click(function () {
        var select = $("<select>", {
            name: "newSelect"
        })
        $(select).append($("<option>", {
            text: "myNewOption",
            value: "myNewValue"
        }))
        $("#myform").append(select)
    })
})

Check out this fiddle: 看看这个小提琴:

https://jsfiddle.net/bbdkr9g5/1/ https://jsfiddle.net/bbdkr9g5/1/

Basically what you wrote, plus some code to print out the form result in the console. 基本上是您编写的内容,外加一些代码以在控制台中打印出表格结果。 And you had the same name attributes on both your static and your dynamically-inserted elements, which is probably not what you wanted. 而且,在静态元素和动态插入的元素上都具有相同的name属性,这可能不是您想要的。

You should clone the elements and remove the newfields class before adding them to the form. 您应该克隆元素并删除newfields类,然后再将它们添加到表单中。

$('.button').click(function(){
    $('#domaincheckform').append($('.newfields').clone().removeClass('newfields'));
    $('#domaincheckform').submit();
});

Here is a JSFiddle example that skips the actual submit: JSFiddle 这是一个跳过实际提交的JSFiddle示例: JSFiddle

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

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