从另一个选择列表更改intlTelInput的国家/地区

时间:2016-08-16 21:09:19

标签: javascript jquery dropdown intl-tel-input

我想根据另一个选择列表更改在intl-Tel-Input中选择的国家/地区。例如如果在国家选择列表中选择了马来西亚,则应将intl-Tel-Input更改为马来西亚,并应显示其标志和代码。相似如果国家改为美国,国际电话输入应相应改变。

感谢任何帮助。

问候。

2 个答案:

答案 0 :(得分:0)

我只是创建js对象“种类的json格式”,其中包含具有特定名称的所有国家/地区代码,并且一旦国家/地区选择匹配使用javascript动态尝试更改输入占位符

答案 1 :(得分:0)

如果你使用React,这是解决方案

constructor(){
    super()
    this.state = {
        updated:true
    }
}

要跟踪国家/地区的变化情况。

componentWillReceiveProps(nextProps){
    if(this.props.selectedCountry !== nextProps.selectedCountry){
        this.setState({
            updated:false
        })
    }
}

告诉你它现在要改变

componentDidUpdate(nextProps){
    if(this.props.selectedCountry !== nextProps.selectedCountry){
        this.setState({
            updated:true
        })
    }
}

现在改变了。

render(){
    const { selectedCountry } = this.props;
    var countryCode = 'us'
    if(selectedCountry)
        countryCode = selectedCountry.toLowerCase()

    var field = <Field
         className="ibm-fullwidth urx-country"
         name="phone" 
         onInputChange={this.onInputChange}
         component={this.renderPhoneInput} 
         defaultCountry={countryCode}
        />

    return (
        <span>
        {this.state.updated &&
            <span>{field}</span>
        }
        </span>
    )
}

基本上它正在重新呈现国家变化。

相关问题