将选择更改为输入以选择国家/地区? [英] Change select to input on selection of country?

查看:87
本文介绍了将选择更改为输入以选择国家/地区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a country selection field that allows for US and Canada and Europe selections, most of our users will be from US/CA so we defaultly show provinces, however when a user selects Europe we want it to change from a select to an input box. We have

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">');
});

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

But it doesn't seem to be working. The html part is all correct and I checked the names.

推荐答案

将其放在$(function())内,以便您的.change()函数在页面加载时绑定:

Put it inside a $(function()) so your .change() functions are bound on page load:

$(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">');
    });
}
);

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

If that doesn't work then you should post your HTML. Are you sure the value attribute of your <select> tag is Europe with a capital E, for example?

这篇关于将选择更改为输入以选择国家/地区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆