Jquery 将输入转换为选择
我希望使用Jquery将输入转换为选择,同时从输入中获取值并将其用作选择选项。但在下面的代码中,到目前为止我只完成了一半。有人能看出哪里出了问题吗?或者建议另一种方法 HTMLJquery 将输入转换为选择,jquery,select,input,option,Jquery,Select,Input,Option,我希望使用Jquery将输入转换为选择,同时从输入中获取值并将其用作选择选项。但在下面的代码中,到目前为止我只完成了一半。有人能看出哪里出了问题吗?或者建议另一种方法 HTML JS $(“.inputDiv输入[type=text]”)。每个(函数(){ 变量数量=$(this.val(); var currentId=$(this.attr('id'); 警报(数量+“是一个数字”); 警报(currentId+“是一个ID”); $(此)。替换为(“”)+ '1' + '2' + '3
JS
$(“.inputDiv输入[type=text]”)。每个(函数(){
变量数量=$(this.val();
var currentId=$(this.attr('id');
警报(数量+“是一个数字”);
警报(currentId+“是一个ID”);
$(此)。替换为(“”)+
'1' +
'2' +
'3' +
'4' +
'5' +
'');
$(“option[value=“+this.qty+”]).attr('selected','selected');
});
这样做:
在DOM中:使id唯一
<div class="inputDiv"><input type="text" id="input01" value="1" name="input01"/></div>
<div class="inputDiv"><input type="text" id="input02" value="4" name="input01"/></div>
<div class="inputDiv"><input type="text" id="input03" value="2" name="input01"/></div>
<div class="inputDiv"><input type="text" id="input04" value="1" name="input01"/></div>
<div class="inputDiv"><input type="text" id="input05" value="4" name="input01"/></div>
在javascript中:
$(".inputDiv input[type=text]").each(function() {
var qty = $(this).val();
var currentId = $(this).attr('id');
alert(qty + " is a Number");
alert(currentId + " is a ID");
$(this).replaceWith('<select id="' + currentId + '" name="" class="NewSelect">' +
'<option value="1">1</option>' +
'<option value="2">2</option>' +
'<option value="3">3</option>' +
'<option value="4">4</option>' +
'<option value="5">5</option>' +
'</select>');
//this qty instead of this.qty
//search for an option within the current div.
$("#" + currentId + " option[value=" + qty + "]").attr('selected', 'selected');
});
$(“.inputDiv输入[type=text]”)。每个(函数(){
变量数量=$(this.val();
var currentId=$(this.attr('id');
警报(数量+“是一个数字”);
警报(currentId+“是一个ID”);
$(此)。替换为(“”)+
'1' +
'2' +
'3' +
'4' +
'5' +
'');
//此数量而不是此数量
//在当前div中搜索一个选项。
$(“#”+currentId+”选项[value=“+qty+”]).attr('selected','selected');
});
$(“.inputDiv输入[type=text]”)。每个(函数(){
var数量=此值;
var currentId=this.id;
var selectbox=$('尝试以下操作:
$("input[type=text]").each(function() {
$(this).parent().replaceWith('<option id=' + this.id + ' value='+ this.value + ' name=' + this.name + ">" + this.value +"</option>")
})
$('option').wrapAll('</select>')
$(“输入[type=text]”)。每个(函数(){
$(this).parent().replaceWith('id's必须在域中是唯一的谢谢,它意外地复制粘贴了,编辑了上面的示例代码谢谢,但我需要输入成为select box@rsplak有正确的想法你甚至看过我发布的JSFIDLE了吗?)呵呵,ic moo tools是默认的加载程序,将其更改为jquery以查看结果。虽然选项没有被选择d、 干杯
$(".inputDiv input[type=text]").each(function() {
var qty = $(this).val();
var currentId = $(this).attr('id');
alert(qty + " is a Number");
alert(currentId + " is a ID");
$(this).replaceWith('<select id="' + currentId + '" name="" class="NewSelect">' +
'<option value="1">1</option>' +
'<option value="2">2</option>' +
'<option value="3">3</option>' +
'<option value="4">4</option>' +
'<option value="5">5</option>' +
'</select>');
//this qty instead of this.qty
//search for an option within the current div.
$("#" + currentId + " option[value=" + qty + "]").attr('selected', 'selected');
});
$(".inputDiv input[type=text]").each(function() {
var qty = this.value;
var currentId = this.id;
var selectbox = $('<select id="'+currentId+'><option value="-1"></select>');
$(this).replaceWith(selectbox);
for(var i = 1; i < 6; i++) {
var option = document.createElement('option');
option.value = i;
$(option).text(i);
if(i==qty) option.setAttribute('selected','selected');
selectbox.append($(option));
}
});
$("input[type=text]").each(function() {
$(this).parent().replaceWith('<option id=' + this.id + ' value='+ this.value + ' name=' + this.name + ">" + this.value +"</option>")
})
$('option').wrapAll('</select>')