admin管理员组文章数量:1291122
HTML
<input type="text" value="" id="ip1" class="ip1" />
<input type="button" value="Add" class="bt1" id="bt1">
</br>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</select>
JQUERY
$(document).ready(function(e) {
$(".bt1").click(function(){
var opt = $("#ip1").val();
});
});
Hi friends here i want to add value from text box to select option using jquery, i got the value from textbox but don't know how to insert, help me.
HTML
<input type="text" value="" id="ip1" class="ip1" />
<input type="button" value="Add" class="bt1" id="bt1">
</br>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</select>
JQUERY
$(document).ready(function(e) {
$(".bt1").click(function(){
var opt = $("#ip1").val();
});
});
Hi friends here i want to add value from text box to select option using jquery, i got the value from textbox but don't know how to insert, help me.
Share Improve this question edited Jul 10, 2014 at 6:12 Suresh Ponnukalai 14k5 gold badges35 silver badges54 bronze badges asked Jul 10, 2014 at 6:05 SMKSMK 652 silver badges7 bronze badges 1- jsfiddle/arunpjohny/hj5u8/1 – Arun P Johny Commented Jul 10, 2014 at 6:11
7 Answers
Reset to default 5you can do like this:
HTML:
<select id="List">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</select>
JQUERY:
$(".bt1").click(function(){
var opt = $("#ip1").val();
$('#List')
.append($("<option></option>")
.attr("value",opt )
.text(opt));
});
FIDDLE DEMO
Use append
$(document).ready(function(e) {
$(".bt1").click(function(){
var opt = $("#ip1").val();
$("select").append('<option value="' + opt+ '">' + opt +'</option>')
});
});
$(document).ready(function (e) {
$(".bt1").click(function () {
var opt = $("#ip1").val();
$('select').append(' <option value="' + opt + '">' + opt + '</option>')
});
});
DEMO
$(".bt1").on('click',function () {
var optionval= $("#ip1").val();
$('select').append(' <option value="' + opt + '">' + opt + '</option>')
});
$(document).ready(function(e) {
$("select").change(function(){
var opt = $("#ip1").val();
});
});
Try this:
$(".bt1").click(function () {
var opt = $("#ip1").val();
//check if option already exists in the drop down
if (!$("select").find("option[value='" + opt + "']").length) {
//add option to the drop down
$("select").append("<option value='" + opt + "'>" + opt + "</option>");
}
//select entered option
$("select").find("option[value='" + opt + "']").attr("selected", "selected");
});
See DEMO here.
Hope this will help
$(document).ready(function (e) {
var $txtVal = $('#ip1');
$(".bt1").click(function () {
var opt = $("#ip1").val();
if($txtVal.val()){
$('<option />', { text: $txtVal.val(),value: $txtVal.val()}).appendTo('select');
}
});
});
FIDDLE HERE >>
本文标签: javascriptHow to insert values in textbox to select optionStack Overflow
版权声明:本文标题:javascript - How to insert values in textbox to select option - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741504490a2382239.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论