Javascript 接收用户字符串输入文本并以JSON格式发布的Ajax脚本

Javascript 接收用户字符串输入文本并以JSON格式发布的Ajax脚本,javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,我有一个函数ipcfg_set,它将设置IP地址,它接受用户输入变量并以JSON格式将其发送回函数。在构建脚本时遇到问题,该脚本接受用户输入的IP、掩码、网关,单击按钮将返回JSON中的值 function ipcfg_set () { $.ajax({ type: 'POST', url: 'ipcfg_set.cgi', dataType

我有一个函数ipcfg_set,它将设置IP地址,它接受用户输入变量并以JSON格式将其发送回函数。在构建脚本时遇到问题,该脚本接受用户输入的IP、掩码、网关,单击按钮将返回JSON中的值

function ipcfg_set () 
               {
                $.ajax({
               type: 'POST',
               url: 'ipcfg_set.cgi',
               dataType : "json",
               data: { ipv4_addr: $('#ip1').val() + '.' + $('#ip2').val() + '.' + $('#ip3').val() + '.' + $('#ip4').val(),
                    gw_addr: $('#gw1').val() + '.' + $('#gw2').val() + '.' + $('#gw3').val() + '.' + $('#ip4').val(), 
                    ipv4_mask: $('#nm1').val() + '.' + $('#nm2').val() + '.' + $('#nm3').val() + '.' + $('#nm4').val()      
                    }
            success: function(data) {

        });
    }


    $(function() {
        $('#btnreset').on('click', ipcfg_get);
        $('#btnapply').on('click', ipcfg_set);
        ipcfg_get();
    });
HTML代码

 <section>

        <h2>IP Configuration</h2>
        <p>Please fill in the form and press <b>Apply</b> to save settings.</p>
        <p>Press <b>Reset</b> to revert settings.</p>
        <section>

        <table class="formgrid">
        <tr><th>IP Address:</th><td><input type="text" name="a1" id="ip1" size="3" value="0">.
                <input type="text" name="a2" size="3" id="ip2" value="0">.
                <input type="text" name="a3" size="3" id="ip3" value="0">.
                <input type="text" name="a4" size="3" id="ip4" value="0"></td></tr>



                <tr><th>Network mask:</th><td><input type="text" name="m1" id="nm1" size="3" value="0">.
                <input type="text" name="m2" size="3" id="nm2" value="0">.
                <input type="text" name="m3" size="3" id="nm3" value="0">.
                <input type="text" name="m4" size="3" id="nm4" value="0"></td></tr>

            <tr><th>Gateway:</th><td><input type="text" name="g1" id="gw1" size="3" value="0">.
                <input type="text" name="g2"  size="3" id="gw2" value="0">.
                <input type="text" name="g3" size="3" id="gw3" value="0">.
                <input type="text" name="g4" size="3" id="gw4" value="0"></td></tr>
        </table>
        </section>
        <section>
        <input id="btnreset" type="button" class="button" value="Reset" />
        <input id="btnapply" type="button" class="button" value="Apply..." />
        </section>

IP配置
请填写表格并按“应用”保存设置

按“重置”恢复设置

IP地址:。 . . 网络掩码:。 . . 网关:。 . .
您可以使用这样的函数从输入中获取值,以减少键入。每个IP地址有四个输入,函数将把值组合成单个字符串:

function formatIP(id) { 
    return $(id+'1').val() + '.' + $(id+'2').val() + '.' + 
           $(id+'3').val() + '.' + $(id+'4').val() 
}
data
参数应该是如下所示的javascript对象:

data: { 
   ipv4_addr: formatIP('#ip'),
   gw_addr:   formatIP('#nm'),
   ipv4_mask: formatIP('#gw')
}

你能显示你的html吗?对于html结构,我有一些带有单个id的表行,希望将它们的字符串文本输入转换为JSON并将值发布到服务器。@jackotonye我根据HTML@jackotonye抱歉,有一个旧版本的功能,我更新了答案。它应该是
function formatIP(id){}
并且它在里面添加了1,2,3,4,所以你可以把它称为
formatIP('#ip')
@jackotonye,但它实际上对这个问题并不重要,你甚至可以不使用额外的函数,比如
数据:{ipv4_addr:$('#ip1').val()+'.+$('#ip2').val()+'.+$('ip2').val()+'.+$('#ip3').val(),…}
成功后,我想更新用户输入值并将设置的ip更改为输入值