Js 向表单中添加多个元素

@{
    ViewBag.title = "地图导航";
}

@model  YT.XWAJ.Public.Application.MapNavigation.Dto.MapNavigationManagementDto
<div class="easyui-layout" data-options="fit: true,border: false">
    <div data-options="region:‘north‘" style="height:80px">
        <div class="form-show">
            <div class="form" style="width: 100%;overflow:auto; padding:10px 15px 5px;">
                <form  id="form" method="post">
                    <div class="form-row col-xs-6 col-sm-6 col-md-6 col-lg-6">
                        <div class="form-row col-xs-6 col-sm-6 col-md-6 col-lg-6">
                            <div class="form-title">导航名称<span class="red"></span></div>
                            <div class="form-value">
                                <input class="easyui-validatebox form-control" type="text" name="MapNavigationName" value="@Model.MapNavigationName" />
                            </div>
                        </div>
                        <div class="form-row col-xs-6 col-sm-6 col-md-6 col-lg-6">
                            <div class="form-title">排序<span class="red"></span></div>
                            <div class="form-value">
                                <input class="easyui-validatebox form-control" type="number" name="Sort" value="@Model.Sort" />
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div region="center">
        <!--地图-->
        <div id="mmap" style="width: 100%;height: 100%;" param="tileUrl:googleWeb;bounds:67.5 0 157.5 67.5;backgroundColor:#FFFEE9;zoom:5;sliceZoom:5;maxZoom:19;minZoom:5;fullZoom:5;mapTilesWidth:2048;mapTilesHeight:1536;errorImg:/scripts/webgis/mxImg/blank.png;mapTilesExtendPixel:0"></div>
    </div>
    <div data-options="region: ‘south‘,height: 45">
        <div class="form-row dlgButRow">
            <div class="pull-left">
                <input type="checkbox" id="chked" checked="checked" class="chk" />确认并关闭窗口
            </div>
            <div class="pull-right">
                <a href="javascript:void(0)" class="btn btn-success" onclick="javascript: window.page.onSave();"><span class="icon icon-save"></span>保存</a>
                <a href="javascript:void(0)" class="btn btn-default" onclick="javascript: window.page.close();"><i class="icon icon-remove"></i>关闭</a>
            </div>
        </div>
    </div>
</div>
@section scripts
{
    <script src="/scripts/jquery/jquery-1.11.3.min.js"></script>
    <script src="/scripts/common/base.js"></script>
    <!-- 地图 -->
    <script src="/scripts/webgis/mxlib/MapHost.js"></script>
    <script src="/scripts/webgis/mxlib/snap.svg-min.js"></script>
    <script src="/scripts/webgis/mxlib/api.min.js"></script>
    <script src="/scripts/webgis/mxlib/Mapv.min.js"></script>
    <script src="/scripts/webgis/mxlib/RectSearchCmd.js"></script>
    <script src="/scripts/map/mapApi.js"></script>
    <script src="/visualization/js/map.js"></script>
    <script type="text/javascript">
        (function () {
            var config = {
                /*是否显示CAD*/
                showCAD: false,
                /*地图显示模型*/
                mapModel: ‘satellite‘, //"web","satellite","proCad"
                /*地图初始参数*/
                mapParams: {
                    x: 119.47353798201721, y: 36.84729076181995,
                    zoom: 12, maxZoom: 14,
                    control: { enabled: true, position: MX_ANCHOR_TOP_LEFT }
                },
                labelStyle: {
                    background: ‘transparent‘,
                    color: "#33ffbb",
                    fontSize: "16px",
                    border: ‘0px solid #ccc‘,
                    height: "20px",
                    lineHeight: "20px",
                    fontFamily: "微软雅黑"

                },
                markerType: {
                    sensor: "传感器",
                    video: "视频",
                    enterprise: "企业",
                    danger: "危化品",
                    hazard: "危险源",
                    shelter: "避难场所",
                    firehouse: "消防队",
                    medical: "医疗机构",
                    communication: "通讯保障",
                    technique: "技术支持",
                    transport: "运输保障"
                }
            };
            var page = {
                load: function () {
                    page.initMap();//显示地图
                },
                //创建和初始化地图函数:
                initMap:function () {
                    var maps = page.maps = new visualMap({
                        el: ‘mmap‘,
                        mapParams: config.mapParams,
                        showCAD: config.showCAD,
                        labelStyle: config.labelStyle,
                        markerType: config.markerType,
                        mapModel: config.mapModel
                    });

                },
                //保存
                onSave: function () {
                    //获取中心点及缩放级别
                    var zoom = page.maps.maps._map.getZoom(),
                        centerPoint = page.maps.maps._map.getCenter();
                    //console.log(zoom);
                    //console.log(centerPoint);
                    var data = $("#form").serializeArray();
                    data.push(
                        {
                            name: ‘Hierarchy‘,
                            value:zoom
                        },{
                            name: "CentraLongitude",
                            value: centerPoint.x
                        }, {
                            name: "CenterLatitude",
                            value: centerPoint.y
                        }
                    );
                    console.log(data);

                    /*
                    $.post(‘/api/services/public/mapNavigation/EditMapNavigationManagement‘,
                        {
                            MapNavigationName: $(‘input[name="MapNavigationName"]‘).val(),
                            CentraLongitude: centerPoint.x,
                            CenterLatitude: centerPoint.y,
                            Hierarchy: zoom,
                            Sort: $(‘input[name="Sort"]‘).val()
                        },
                        function (result) {
                            $.closeParentWindow();
                        });
                        */

                    $.ajax({
                        type: "POST",
                        url: ‘/api/services/public/mapNavigation/EditMapNavigationManagement‘,
                        data: data,
                        success: function (result) {
                            $.closeParentWindow();
                        }
                    });

                },

                //刷新
                onRefresh: function() {
                    $("#datagrid").datagrid(‘reload‘);
                },
                //取消
                close: function () {
                    $.closeParentWindow();
                }
            };
            $(page.load);
            window.page = page;
        }());
    </script>
}

上边代码无法正常运行。

但是期间遇到一个问题:就是页面只把表单中的数据遍历了,地图中获取的值没有传回后台, 因此我们有两种办法可以实现将值传回后台: 方法如下

1、这种方式如果穿的之多的情况下 不太好。

$.post(‘/api/services/public/mapNavigation/EditMapNavigationManagement‘,
     {
       MapNavigationName: $(‘input[name="MapNavigationName"]‘).val(),
       CentraLongitude: centerPoint.x,
       CenterLatitude: centerPoint.y,
       Hierarchy: zoom,
       Sort: $(‘input[name="Sort"]‘).val()
     },
  function (result) {
       $.closeParentWindow();
  });

2、下边的方法是给data.push元素  以下push了三个对象。 (需看接口返回的格式)

//保存
onSave: function () {
      //获取中心点及缩放级别
      var zoom = page.maps.maps._map.getZoom(),
      centerPoint = page.maps.maps._map.getCenter();
      //console.log(zoom);
      //console.log(centerPoint);
      var data = $("#form").serializeArray();
      data.push(
           {
               name: ‘Hierarchy‘,
               value:zoom
           },{
               name: "CentraLongitude",
               value: centerPoint.x
           }, {
               name: "CenterLatitude",
               value: centerPoint.y
           }
      );
      console.log(data);
      $.ajax({
           type: "POST",
           url: ‘/api/services/public/mapNavigation/EditMapNavigationManagement‘,
           data: data,
           success: function (result) {
             $.closeParentWindow();
         }
      });
}

以上代码均可实现给表单添加对象元素。

时间: 2024-08-03 19:03:14

Js 向表单中添加多个元素的相关文章

在HTML文件的表单中添加{%csrf_token%}便可以解决问题

原因是django为了在用户提交表单时防止跨站攻击所做的保护 只需在HTML文件的表单中添加{%csrf_token%}便可以解决问题 ------------------------if判断{% if %}标签计算一个变量值,如果是"true",即它存在.不为空并且不是false的boolean值,系统则会显示{% if %}和{% endif %}间的所有内容 {% if num >= 100 %} {% if num > 200 %} <p>num大于20

php 在同一个表单中添加和修改

大家写网站的时候可能都会遇到这种情况,就是写一个表单,这个表单是用来添加一篇文章的,我们屁颠屁颠的在后台接收数据,然后存入数据库.现在有个问题.当你要对该文章进行修改的时候,你是怎么处理的? 我的方法是在同一个表单中做文章,下面的例子是我用 Thinkphp框架写的代码,如果你不懂Thinkphp,没有关系,我只是想说说我的解决方案. 前台如果是这样的. <form method="POST" action="{:U('article/add')} > <i

Js实现表单中的checkbox对勾选中效果

<!doctype html> <html lang="en-US"> <head> <meta http-equiv="Content-Type" content="text/html; chaRset=gb2312" /> <title>JS实现单个图片选中美化框</title> <style type="text/css"> body,

php 在同一个表单中添加和修改数据 二

好吧,其实我这人不看重访问量的,但是今天一天的访问量比我去年发的一篇还要多. 我还是有点小小的惊讶的.作为一个做技术的屌丝.不,我不认为自己是屌丝,我觉得编程是一件高大山的职业.虽然很累,但是确实能让你每天的生活很充实. No matter whether you believe it or not,anyhow I believe it. 今天接着上一篇来说,我们只是简单的文章的上传和修改. 如果涉及到附件呢.在修改的时候如果需要更换附件的时候,这真的是一件很头痛的事情. 我不知道大家是怎样实

Spring Security在标准登录表单中添加一个额外的字段

概述 在本文中,我们将通过向标准登录表单添加额外字段来实现Spring Security的自定义身份验证方案. 我们将重点关注两种不同的方法,以展示框架的多功能性以及我们可以使用它的灵活方式. 我们的第一种方法是一个简单的解决方案,专注于重用现有的核心Spring Security实现. 我们的第二种方法是更加定制的解决方案,可能更适合高级用例. 2. Maven设置 我们将使用Spring Boot启动程序来引导我们的项目并引入所有必需的依赖项. 我们将使用的设置需要父声明,Web启动器和安全

js+jquery动态设置/添加/删除/获取元素属性的两种方法集锦对照(动态onclick属性设置+动态title设置)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html140/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>

js数组指定位置添加和删除元素

//按指定位置删除Array.prototype.removeIndex = function (index) { if (index > - 1) { this.splice(index, 1); } };//按元素名称删除 Array.prototype.remove = function (val) { var index = this.indexOf(val); if (index > - 1) { this.splice(index, 1); } };//添加元素到指定位置 Arra

js 交换表单中值

<html> <head> <meta charset="utf-8" /> <script type="text/javascript"> window.onload=function(){ var btn=document.getElementById('btn'); var text=document.getElementById('text'); var sele=document.getElementById

js 提交表单添加csrf

function post(path, shipmentMap, method) { method = method || "post"; // Set method to post by default if not specified. var token = $('meta[name="_csrf"]').attr('content'); var tokenName = $('meta[name="_csrf_header"]').attr