Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

javascript - jQuery按ID查找元素作为变量(Jquery find element by id as variable)

Im using jquery to get a list of objects from the server.

(我正在使用jquery从服务器获取对象列表。)

Every object has two fields id and singleValue (on the server both of them are Strings).

(每个对象都有两个字段idsingleValue (在服务器上,它们都是String)。)

I want to iterate over the list and put values in the correct elements.

(我想遍历列表并将值放入正确的元素中。)

for(var i=0;i<response.listOfObjects.length;i++){
    $(response.listOfObjects[i].id).val(response.listOfObjects[i].singleValue);
}

Nothing happend by running this code.

(运行此代码没有任何反应。)

List has correct values (checked it by alert("id: " + response.listOfObjects[i].id + " singleValue: " + response.listOfObjects[i].singleValue); )

(列表具有正确的值(通过alert("id: " + response.listOfObjects[i].id + " singleValue: " + response.listOfObjects[i].singleValue); ))

I also tried to do

(我也试着做)

$('#' + response.listOfObjects[i].id).val(response.listOfObjects[i].singleValue);

but still doesn't work.

(但仍然不起作用。)

How is it possible to solve this problem?

(如何解决这个问题?)

@EDIT the html looks like:

(@EDIT html看起来像:)

<div class="field-group" style="display: block;" original-title="" title="">
   <label for="customfield_13307">PCN</label>
   <input class="textfield text long-field" id="customfield_13307" name="customfield_13307" maxlength="254" type="text" value="">
   <div class="description">EPCN</div>
</div>

The id from the object is the id of the input.

(的id从对象是输入的ID。)

@EDIT I used JSON.stringify(response):

(@EDIT我使用了JSON.stringify(response):)

    {
   "name":"ATB",
   "listOfObjects":[
      {
         "id":"customfield_13305",
         "type":"single",
         "singleValue":"21766"
      },
      {
         "id":"customfield_13307",
         "type":"Text",
         "singleValue":" aaaa"
      },
      {
         "id":"customfield_13308",
         "type":"Text",
         "singleValue":" bbbb"
      }
   ]
}
  ask by Rodnev Dlog translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based on author's code, I assume, that the expected output is [{id:singleValue}] .

(根据作者的代码,我假设预期的输出为[{id:singleValue}] 。)

In this case I suggest you to use Array.reduce() method which perfectly does work for you.

(在这种情况下,我建议您使用完全适合您的Array.reduce()方法。)

I've tried to add few comments in the code:

(我试图在代码中添加一些注释:)

 const response = { // sample response for testing purpose listOfObjects: [{ id: 'id1', singleValue: 1 }, { id: 'id2', singleValue: 2 }, { id: 'id3', singleValue: 3 }, ] } const mapped = response.listOfObjects.reduce(function(accumulator, currValue) { // here goes simple object transformation let obj = {}; obj[currValue.id] = currValue.singleValue; accumulator.push(obj); return accumulator; // each iteration should retrun accumulator }, []); // here you define starting object for your reduce method console.log(mapped); 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...