How can I dynamically add a select element to a form using Prototype & Rails?

爱⌒轻易说出口 提交于 2020-01-06 06:10:28

问题


I have generated the code for a select & its objects using Rails:

var insert_tag = '<select class="select medium" id="category_id" name="search[category_id]"><option value="-1">Select Category</option>
<option value="1">Boats and Cruisers</option>
<option value="2">Car Parts and Spares</option>
<option value="3">Caravans</option>
<option value="4">Coaches and Buses</option>
<option value="5">Commercial Parts</option>
<option value="6">Commercials</option>
<option value="7">Crash Repair Services</option></select>';

I have on JavaScript function which when called should insert this field into a blank div:

<div "form_field_a"></div>


$("form_field_a").insert(insert_tag);

But when this runs I just get this error:

content.toElement is not a function
[Break on this error] if (content && content.toElement) content = content.toElement(); 

I am new to JavaScript and have no idea why this does not work.


回答1:


I don't know Prototype, but your div element is missing an id attribute (currently it is invalid HTML):

<div id="form_field_a"></div>

Then it seems to work: http://jsfiddle.net/KqVdf/1

Also note that the value of insert_tag is one line without line breaks (got an error otherwise).



来源:https://stackoverflow.com/questions/3826235/how-can-i-dynamically-add-a-select-element-to-a-form-using-prototype-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!