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
523 views
in Technique[技术] by (71.8m points)

jquery - HTML + Add input field inside a dropdown box

A new idea came across my mind.Someone may have implemented this. I am trying to get a way to add a textbox inside a dropdown list.

Means I want to add a input box inside a dropdown list. For example :

<select id='country'>
  <option value='USA'>USA</option>
  <option value='UK'>UK</option>
  <option value='Russia'>Russia</option>
  <option><input type='text' /></option> // Here I want to add dynamic country 
                                            using ajax but I am able to render
                                            this dropdown.
</select>

Does anyone have any idea on how to execute this? I do not want to use any modal box or input box outside this dropdown. I just want to add the value inside the dropdown and press the enter key to add in DB. I'm not worried about the server-side code. I want to be able to render the dropdown only.

Thanks in advance.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

No. You cannot do that. Option cannot contain any other elements other than text content.

You may instead want to look at some custom plugins, or create your own to render div/spans out of the select that you provide.

Permitted content Text with eventually escaped characters (like é).

Just one example using bootstrap's dropdown. You can customize and style it accordinf to your need.

Fiddle

Little bit of customization like this

$('.dropdown-menu li').click(function () {
     $('.dropdown-toggle b').remove().appendTo($('.dropdown-toggle').text($(this).text()));
 });

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

...