使用jquery扩展对象 [英] Extend an object using jquery

查看:134
本文介绍了使用jquery扩展对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个div,其中第1个div有2个文本框,而另一个div有几个文本框

手动我将文本框名称和值添加到第1个div的对象。但对于第二个div,我使用jQuery获取所有文本框名称和值,以避免使用大块相似的代码。



I have 2 divs where 1st div has 2 text boxes while the other div has several text boxes
Manually I am adding the textbox name and value to an object for the 1st div. But for second div I am getting all the textbox names and values using jQuery to avoid having a large block of similar code.

var obj = {};
obj.LayoutName = document.getElementById("aSL_stripLayoutName").value;
obj.Version = document.getElementById("aSL_StripLayoutVersion").value;





然后我想扩展对象和使用extend将所有文本框名称和值添加到'obj',




Then I want to extend the object and add all textbox names and values to 'obj' using extend,

$("#aSL_stripLayoutDetails-UpperSection :text").each(function(){//TODO}); 





我的尝试:



我已经尝试了下面的例子,正在添加一个已知的文本框名称'StripDimX'和值'333'



What I have tried:

I have tried the below and an example is working adding a known textbox name 'StripDimX' and value '333'

$.extend(obj, {StripDimX : "333"});





如果我想得到这个名字,我在下面的函数中使用它



If I want to get the name I use this inside the below function

document.getElementById(this.id).name





这是价值



and this for the value

document.getElementById(this.id).value





但我的问题是如何添加名称和值到对象?



But my problem is how to add the names and values to the object?

$("#aSL_stripLayoutDetails-UpperSection :text").each(function(){
   //$.extend(obj, {StripDimX : "333"});
});



这当然不起作用:


This of course does not work:

$("#aSL_stripLayoutDetails-UpperSection :text").each(function(){
 $.extend(obj, {document.getElementById(this.id).name:             document.getElementById(this.id).value});
});





然后我尝试了以下,名称没问题,但价值是作为数组添加的:





Then I tried the below and name is ok, but the value is being added as an array:

$("#aSL_stripLayoutDetails-UpperSection :text").each(function(){
        var n = document.getElementById(this.id).name;
        var v = document.getElementById(this.id).value;
          $.extend(obj, { [n] : [v]});
});

推荐答案

(#aSL_stripLayoutDetails-UpperSection:text)。each(function(){// TODO}) ;
("#aSL_stripLayoutDetails-UpperSection :text").each(function(){//TODO});





我的尝试:



我已经尝试了下面的例子,正在添加一个已知的文本框名称'StripDimX'和值'333'



What I have tried:

I have tried the below and an example is working adding a known textbox name 'StripDimX' and value '333'


。 extend(obj,{StripDimX:333});
.extend(obj, {StripDimX : "333"});





如果我想得到名字,我在下面的函数中使用它



If I want to get the name I use this inside the below function

document.getElementById(this.id).name





和this for价值



and this for the value

document.getElementById(this.id).value





但是我的问题是如何将名称和值添加到对象?



But my problem is how to add the names and values to the object?


(#aSL_stripLayoutDetails-UpperSection:text)。 each(function(){
//
("#aSL_stripLayoutDetails-UpperSection :text").each(function(){ //


这篇关于使用jquery扩展对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆