1

我想根据另一个选择列表更改在 intl-Tel-Input 中选择的国家。例如,如果在国家选择列表中选择了马来西亚,则 intl-Tel-Input 应更改为马来西亚,并应显示其标志和代码。同样,如果将国家更改为美国,则 intl-Tel-Input 应相应更改。

任何帮助表示赞赏。

问候。

4

2 回答 2

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>
    )
}

基本上它正在重新渲染国家/地区的变化。

于 2017-08-21T11:29:50.080 回答
0

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

于 2016-08-16T21:15:39.647 回答