• <bdo id='jyjmi'></bdo><ul id='jyjmi'></ul>

      <small id='jyjmi'></small><noframes id='jyjmi'>

    1. <i id='jyjmi'><tr id='jyjmi'><dt id='jyjmi'><q id='jyjmi'><span id='jyjmi'><b id='jyjmi'><form id='jyjmi'><ins id='jyjmi'></ins><ul id='jyjmi'></ul><sub id='jyjmi'></sub></form><legend id='jyjmi'></legend><bdo id='jyjmi'><pre id='jyjmi'><center id='jyjmi'></center></pre></bdo></b><th id='jyjmi'></th></span></q></dt></tr></i><div id='jyjmi'><tfoot id='jyjmi'></tfoot><dl id='jyjmi'><fieldset id='jyjmi'></fieldset></dl></div>

      <tfoot id='jyjmi'></tfoot>

        <legend id='jyjmi'><style id='jyjmi'><dir id='jyjmi'><q id='jyjmi'></q></dir></style></legend>

        将属性动态分配给 JS 对象

        时间:2024-04-19

          <tbody id='JB014'></tbody>
        <i id='JB014'><tr id='JB014'><dt id='JB014'><q id='JB014'><span id='JB014'><b id='JB014'><form id='JB014'><ins id='JB014'></ins><ul id='JB014'></ul><sub id='JB014'></sub></form><legend id='JB014'></legend><bdo id='JB014'><pre id='JB014'><center id='JB014'></center></pre></bdo></b><th id='JB014'></th></span></q></dt></tr></i><div id='JB014'><tfoot id='JB014'></tfoot><dl id='JB014'><fieldset id='JB014'></fieldset></dl></div>

          <small id='JB014'></small><noframes id='JB014'>

          1. <tfoot id='JB014'></tfoot>
              <legend id='JB014'><style id='JB014'><dir id='JB014'><q id='JB014'></q></dir></style></legend>
                • <bdo id='JB014'></bdo><ul id='JB014'></ul>
                  本文介绍了将属性动态分配给 JS 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要为一个 js 对象动态添加属性,这是我通过 eval() 实现的:

                  I needed to dynamically add properties to an js Object, which I achieved via eval():

                  $ ->
                  
                    #Methods
                    window.add_address = (attributes, id=new Date().getTime())->
                      $container = $('ul#addresses_list')
                      $unit = $('<li>')
                  
                      $.each attributes, (key,value)->
                        $input = $('<input type="hidden">')
                        $input.attr 'name', "contact[addresses_attributes][#{id}][#{key}]"
                        $input.val value
                        $unit.append $input
                  
                      $container.append $unit
                  
                    #Events
                  
                    #Add address button
                    $('a#add_address').on 'click', (ev)->
                      attributes =  new Object
                      $('#address_fields').find('input').each ->
                        eval("attributes.#{$(this).attr 'id'}='#{$(this).val()}'");
                  
                      add_address attributes
                  

                  这很完美,但我觉得 eval() 很尴尬,有没有办法让这个更漂亮"?我的意思是,我搜索了像 eval() 这样的替代方法a href="http://api.jquery.com/serializeArray/" rel="nofollow">Jquery .serializeArray() 但它似乎只适用于查询表单,我需要从那个 #address_fields div.

                  This works perfect but I feel awkward with the eval(), is there anyway to do this "prettier"? I mean, I searched for alternatives like the Jquery .serializeArray() but it seems to work only with a queried form and I need to get the inputs from that #address_fields div.

                  推荐答案

                  使用 object['key'] 表示法

                  attributes[$(this).attr('id')] = $(this).val();
                  

                  使用以下方法创建对象也非常有效:

                  It's also very efficient to create object using:

                  var attributes={};
                  

                  类似的行可以用相同的符号编写 jquery 方法

                  Along similar lines can write the jquery methods in same notation

                     attributes[$(this)['attr']('id')] = $(this)['val']();
                  

                  这篇关于将属性动态分配给 JS 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:按名称调用函数 下一篇:AngularJS:初始复选框值不在模型中

                  相关文章

                  <small id='lS1u1'></small><noframes id='lS1u1'>

                    <bdo id='lS1u1'></bdo><ul id='lS1u1'></ul>
                  <legend id='lS1u1'><style id='lS1u1'><dir id='lS1u1'><q id='lS1u1'></q></dir></style></legend>

                    <i id='lS1u1'><tr id='lS1u1'><dt id='lS1u1'><q id='lS1u1'><span id='lS1u1'><b id='lS1u1'><form id='lS1u1'><ins id='lS1u1'></ins><ul id='lS1u1'></ul><sub id='lS1u1'></sub></form><legend id='lS1u1'></legend><bdo id='lS1u1'><pre id='lS1u1'><center id='lS1u1'></center></pre></bdo></b><th id='lS1u1'></th></span></q></dt></tr></i><div id='lS1u1'><tfoot id='lS1u1'></tfoot><dl id='lS1u1'><fieldset id='lS1u1'></fieldset></dl></div>
                  1. <tfoot id='lS1u1'></tfoot>