我想将表单输入作为变量

问题描述 投票:0回答:2
 <form id="notification_form" >
        <div style="display: flex;" id="xx">


        <div style="float: left; width: 40%;margin-left: 1%;">

      <label style="margin-top: -2px">message text on area entrance</label>
        <input style="margin-top: -11px" type="text" id="notf_msg_entrance">

            <label style="margin-top: -11px">message text on area exit</label>
        <input style="margin-top: -11px" type="text" id="notf_msg_exit">
        </div>
        <div style="width: 40%;margin-left: 10%;margin-top: 12px;" >
<!--        <div><input type="checkbox" name="push" value="1"  id="notf_push"> <label>push </label></div>-->
            <div id="right_div">
                <select class='form-control inputstl' id="select_aud"></select>
            <label style="margin-left: 7px;">sms notification</label>
        <input type="tel" id="notf_tel_num" class='form-control inputstl'>
            <label style="margin-left: 7px;">mail notification</label>
        <input type="mail" id="notf_mail" class='form-control inputstl'>
            <div>
              <input style="margin-left: 2px" type="checkbox" class="emergency" value="1" > 
              <label style="float: right;margin-top: -1px;margin-right: 25px;">emergency notification</label></div>
        </div>
            </div>

            </div>
            </form>

当我制作console.log($('#notification_form').serialize());时它没有给我回应,而它给了我另一种形式

javascript jquery html forms
2个回答
1
投票

所有表单元素都需要name属性才能包含在最终提交的表单数据中。如果它们不包含此项,则在准备要发送到服务器的表单数据时,浏览器将忽略它们。

同样的规则适用于jQuery的serialize()方法,因为它试图应用浏览器在不使用JavaScript的情况下提交表单时使用的相同逻辑。

例如你的第一个输入框可以改为这样的东西:

<input style="margin-top: -11px" type="text" id="notf_msg_entrance" name="msg_entrance">

将名称设置为您希望在传输到服务器时显示的名称。将此应用于表单中的所有表单元素(输入(包括文本框,复选框,无线电),选择,textareas等。


1
投票

您的输入需要名称属性。

<input style="margin-top: -11px" type="text" name="notf_msg_entrance" id="notf_msg_entrance">

https://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax_serialize

© www.soinside.com 2019 - 2024. All rights reserved.