jquery onchange select value has a period on it
I have a select field like so.
<select id="v_bl_title" name="v_bl_title">
<option selected="" value="Mr.">Mr.</option>
<option value="Mrs.">Mrs.</option>
<option value="Ms.">Ms.</option>
<option value="Miss.">Miss</option>
<option value="">None</option>
</select>
$('#v_bl_title').bind('change',function(){
var v_bl_title = $(this).val();
开发者_如何学运维 $(#v_sh_title option[value=' + v_bl_title + ']').attr('selected', 'selected');
});
I am trying to copy the value over, but I cant get it to work with the period in the value.
I get the error uncaught exception: Syntax error, unrecognized expression: [value=Ms.]
Is there a way to fix this?
Thanks!
$('#v_bl_title').bind('change',function(){
var v_bl_title = $(this).val();
$("#v_sh_title option[value='" + v_bl_title + "']").attr('selected', 'selected');
});
http://jsfiddle.net/UGbm4/2/
or you can simply use val
$('#v_bl_title').bind('change',function(){
var v_bl_title = $(this).val();
$("#v_sh_title").val(v_bl_title);
});
http://jsfiddle.net/UGbm4/
Yep, use quotation marks: [value="Ms."]
I.e.:
$('#v_bl_title').bind('change',function(){
var v_bl_title = $(this).val();
$('#v_sh_title option[value="' + v_bl_title + '"]').attr('selected', 'selected');
}); // here ----^ and here ----^
精彩评论