jQuery serialize hidden (display:none) form elemens does not work. Workaround?

前端 未结 1 1894
广开言路
广开言路 2021-01-19 06:34

I have the following problem:

I have a form I need to serialize but I am using javascript to change the look of the select fields. This means, the real select fields

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-19 06:46

    If the issue is caused by hidden elements, temporary show them before serializing:

    var $form = $('#myForm');
    var hidden = $form.find(':hidden'); // Select all hidden elements
    hidden.show();                      // Show them
    var string = $form.serialize();     // Serialize form
    hidden.hide();                      // Hide them again
    

    Edit: It seems that you're trying to select an option by setting the selected=selected attribute. You should use selectedIndex to change the selected option:

    var select = $("#myselect")[0]; //DOM element
    select.selectedIndex = 5;       //Example, select 6th option
    

    0 讨论(0)
提交回复
热议问题