更改选择输入选择国家?

Ste*_*ven 3 jquery

我有一个国家选择字段,允许美国,加拿大和欧洲选择,我们的大多数用户将来自美国/加州,因此我们默认显示省份,但是当用户选择欧洲时,我们希望它从选择框更改为输入框.我们有

jQuery('select.country_d').change(function(){
    if($('select.country_d').val() == "Europe")
    $('.state_d').replaceWith('<input type="text" name="state_d" id="state_d">');
});

jQuery('select.country_o').change(function(){
    if($('select.country_o').val() == "Europe")
    $('.state_o').replaceWith('<input type="text" name="state_o" id="state_o">');
});
Run Code Online (Sandbox Code Playgroud)

但它似乎没有奏效.html部分都是正确的,我检查了名称.

chi*_*ley 6

把它放在一个$(function())所以你的.change()函数绑定在页面加载:

$(function()
{
    $('select.country_d').change(function()
    {
        if ($(this).val() == 'Europe')
        $('.state_d').replaceWith('<input type="text" name="state_d" id="state_d">');
    });

    $('select.country_o').change(function(){
        if($(this).val() == 'Europe')
        $('.state_o').replaceWith('<input type="text" name="state_o" id="state_o">');
    });
}
);
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,那么你应该发布你的HTML.例如,您确定标签的value属性<select>是否Europe带有大写字母E?