jQuery selector for select box
i have the follow code
$("#tlbQueryConf tr:last").append(
$('<td><开发者_如何学JAVA/td>').append(
$(".ddlFields").clone().attr('id', 'ddlFields-' + conf.id).attr('class', '').val(conf.field)));
So now, what i'm trying to accomplish it's give the attribute selected
to the new element with that value that i selected
i tried like this, but sets the atribute selected
to the element and not the item
$(".ddlFields").clone().attr('id', 'ddlFields-' + conf.id).attr('class', '').val(conf.field).attr('selected',true)
the html that result its the follow
<select name="ctl00$ContentPlaceHolder1$ddlFields" id="ddlFields-1" class="">
<option value="1">Prueba</option>
<option value="2">cantidad de fallas</option>
<option value="3">nº</option>
<option value="4">Ciudad</option>
<option value="5">COID</option>
<option value="6">COID_DESC</option>
<option value="7">Creado_EN</option>
<option value="8">DSLAM PROVEEDOR</option>
</select>
Note
even using val()
the property selected
it's not set to item
If I understand the question correctly (and correct me if I misunderstood), you want to change the selection of the drop down list?
You can do that like this:
$(".ddlFields").val("value of the item you want selected");
and you can retrieve the selected value like this:
$(".ddlFields").val();
You don't have to worry about setting the selected
attribute your self, jquery handles that for you when you set the value of the list.
See this duplicate question for more information: Change the selected value of a drop-down list with jQuery
You're adding a selected=selected
attribute to the select
element when you should be selecting one of the options. Try this:
$("#tlbQueryConf tr:last").append(
$('<td>', {
html: $(".ddlFields").clone()
.attr({'id':'ddlFields-' + conf.id,'class':''}).val(conf.field)
})
);
http://jsfiddle.net/hunter/aEVt2/
Which item are you trying to select? As per your code it will add selected attribute to the select tag. You should specify the option to select. Try this
$(".ddlFields").clone().attr({ 'id': 'ddlFields-' + conf.id, 'class': ''}).val(conf.field);
精彩评论