我想动态地向我的javascript对象添加值,这就是正在做的事情
options = {"data":{"clave1":"valor1",...}};
我想要的是那个
选项就是这样
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="MsgBox.ascx.vb" Inherits="MsgBox" ClassName="MsgBox" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="act" %>
<style type="text/css">
.Modal_trans {
opacity: 0.8;
filter: alpha(opacity=60);
}
</style>
<link rel="stylesheet" type="text/css" href="../Styles/StyleSheet.css" />"
<asp:Panel ID="PopPanel" runat="server"
Width="500px" Height="50px" DefaultButton="ok" Style="overflow:hidden;" CssClass="ModalGrid">
<table style="width: 100%; border-collapse: collapse; border: 0px;">
<tr id="boxheader" runat="server" style="cursor: move;">
<th colspan="2" style="padding-left: 10px; padding-top: 3px; padding-bottom: 3px;">
<asp:Label ID="lblTitle" runat="server" Text="Help"></asp:Label>
</th>
</tr>
<tr>
<td>
<asp:Image ID="Image" runat="server" ImageUrl="../images/info.png" />
</td>
<td style="padding: 10px; width: 220px;">
<div style="width: 295px; height:70px;"">
<asp:Label ID="lblMsg" runat="server" BackColor="#CCCCCC" />
</div>
</td>
</tr>
<tr>
<th colspan="2" style="padding-right: 20px; padding-top: 7px; padding-bottom: 7px;"" >
<asp:LinkButton ID="LinkButton1" runat="server" Style="display:none" ></asp:LinkButton>
<asp:Button ID="ok" runat="server" Text="Ok" Width="80px" />
<asp:Button ID="no" runat="server" Text="No / Cancel" Width="80px" Visible="False" />
</th>
</tr>
</table>
</asp:Panel>
<act:ModalPopupExtender ID="PopUp" runat="server" Enabled="True"
PopupControlID="PopPanel" TargetControlID="LinkButton1" BackgroundCssClass="Modal_trans"
OkControlID="ok" PopupDragHandleControlID="boxheader" Y="0" DropShadow="True">
<Animations>
<OnShown>
<Sequence AnimationTarget="PopPanel">
<Parallel Duration=".5" Fps="25">
<Move Horizontal="-175" Vertical="200" />
<Resize Width="350" Height="142"/>
<FadeIn />
</Parallel>
</Sequence>
</OnShown>
<OnHiding>
<Sequence AnimationTarget="PopPanel">
<%-- Scale the flyout down to 5% to make it disappear --%>
<Parallel Duration=".75" Fps="25">
<Scale ScaleFactor="0.05" Center="true" ScaleFont="true" FontUnit="px" />
<FadeOut />
</Parallel>
<%-- Reset the styles on the info box --%>
<StyleAction Attribute="display" Value="none"/>
<StyleAction Attribute="width" Value="350px"/>
<StyleAction Attribute="height" Value=""/>
<StyleAction Attribute="fontSize" Value="12px"/>
<%-- Re-enable the button --%>
<EnableAction Enabled="true" AnimationTarget="ok" />
</Sequence>
</OnHiding>
</Animations>
</act:ModalPopupExtender>
答案 0 :(得分:1)
在JS中,您需要使用括号来添加键值对
var data = {
"clave1": "valor1",
"clave2": "valor2"
};
var options = {
"data": {}
};
$.each(data, function(c, v) {
options.data[c] = v;
});
console.log(options)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
您不需要jQuery来执行此操作,您可以使用函数Object.keys
然后使用函数forEach
循环:
var data = {
"clave1": "valor1",
"clave2": "valor2"
};
var options = {
"data": {}
};
Object.keys(data).forEach(k => options.data[k] = data[k])
console.log(options)
&#13;
最后,使用函数Object.assign
var data = {
"clave1": "valor1",
"clave2": "valor2"
};
var options = {
"data": Object.assign({}, data)
};
console.log(options)
&#13;
答案 1 :(得分:0)
将新值附加到对象很简单!对象非常像数组,但可以将字符串作为键!例如:
var obj = {};
obj["data"] = {};
obj["data"]["clave1"] = "valor1";
所以你要做的就是确保你知道参数的名称!你也可以这样做:
var str = "clave1";
var str2 = "valor1";
var obj = {};
obj["data"] = {};
obj["data"][str] = str2;