Skip hidden inputs in Jquery serialize

后端 未结 4 1677
无人及你
无人及你 2021-01-12 18:09

I have forms with inputs of hidden and visible like below.In this example I want to serialize only name=\'country\' which is not hidden

相关标签:
4条回答
  • 2021-01-12 18:52

    You can make a temporary copy of it and remove the hidden inputs from it before serializing :

    var form = $("#finalform").clone();
    $(form).find("input[type=hidden]").remove()
    var serialized = $(form).serializeArray();
    
    0 讨论(0)
  • 2021-01-12 18:55

    Here is another possible solution with a demo.

    var notHidden = $('#finalform > :not(input[type=hidden])').serializeArray();
    
    $('#itemsSerializedCount').text($(notHidden).length)
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/json2/20140204/json2.js"></script>
    <form id="finalform">
    <input type='hidden' name='sid' value='901271316' />
    <input type='hidden' name='tco_currency' value='USD'>
    <input   name='country' value='India' />
    </form>
    
    <lablel id="itemsSerializedCount"></label>

    Here, the input control which is not hidden is serialized.

    0 讨论(0)
  • 2021-01-12 18:56

    I find this a cleaner way of doing it:

    var frm = $('#finalform [type!="hidden"]').serialize()
    
    0 讨论(0)
  • 2021-01-12 19:06

    use below code . not(:hidden) will not include input type hidden in serialize()

    check DEMO

    var frm  = $('#finalform').find(":input:not(:hidden)").serialize();
    

    Or

    var frm  = $('#finalform :input:not(:hidden)').serialize();
    

    DEMO

    var frm  = $('#finalform :input:not(:hidden)').serializeArray();
    
    var frm1  = $('#finalform').find(":input:not(:hidden)").serializeArray();
    
    $('#firstOutput').text(JSON.stringify(frm))
    $('#secondOutput').text(JSON.stringify(frm1))
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <form id="finalform">
     <input type='hidden' name='sid' value='901271316' />
     <input type='hidden' name='tco_currency' value='USD'>
     <input   name='country' value='India' />
    </form>
    
    
    <div id="firstOutput"> </div>
    <div id="secondOutput"> </div>

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