开发者

javascript - get object data

In my rails application, i have a form where user has to select some items via checkbox. I want to display the selected items' information in a div.

I am currently using javascript to do that.

So in form, when user clicks in a checkbox, i pass the 'this' obj as parameter to a javascript function which then appends the 'this.value' to the end the div.

However appending the 'this.value' displays only the id of of the selected item. I also want to display the other data of the selected item in the div.

Is that possible in javascript? How do i get other information of the 'this' obj, apart from 'this.value'?

Many thanks for any suggestion provided.

Please find开发者_如何学编程 code below-

#in my form:
<input id="id_<%= item.id %>" name="submission[item_ids][]" onclick="addtoselected(this);" value="<%= item.id %>" type="checkbox" />

javascript code:

function addtoselected(obj){
    if(obj.checked==true){
    var s = '<div>' + obj.value + '</div>';
    $j('#selected_recs').append(s);
    }
}


If the text you want is in a label, you could loop through all labels and check which one has the id in his for attribute.


Use the contents of this.value to lookup the information you need. You could send it as a parameter to a server-side script to get its information back in JSON format, or if you already have an existing javascript object store, you could look up the item's information from there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜