简体   繁体   English

如何获取已选中(复选框)的值并在本机反应中自动完成 textInput

[英]How can I get the value of checked (checkbox) and autocomplete a textInput in react native

could you help me?, I'm developing with react native and typescript, I need to do this: I have a checkBox that tells the user to check if his name is the same as his credit card, if he clicks the checkbox, a TextInput with his name is autocompleted, if you don't click the checkbox that he can type his name.你能帮我吗?,我正在使用 react native 和 typescript 进行开发,我需要这样做:我有一个复选框,告诉用户检查他的名字是否与他的信用卡相同,如果他点击复选框,a如果您不单击他可以输入他的名字的复选框,则带有他的名字的 TextInput 会自动完成。 I have a variable like: user_name I don't have code because I don't know how to do it, but I think it goes something like this:我有一个变量,例如: user_name 我没有代码,因为我不知道该怎么做,但我认为它是这样的:

 const [checked, setChecked] = useState(false)
...
return(
            <CheckBox
              checked={setChecked}
              onPress={}
              checkedIcon='dot-circle-o'
              uncheckedIcon='circle-o'
              checkedColor='blue'
              uncheckedColor='blue'
            />


        <TextInput
          placeholder='Nombre completo'
          value={}
          onChangeText={}
)

This code should work (run the snippet for demonstration)此代码应该可以工作(运行代码片段进行演示)

 const checkbox = document.getElementById('checkBox') var name = "Tom" var input = document.createElement('input') checkbox.addEventListener('change', (event) => { if (event.currentTarget.checked) { document.querySelector("body").appendChild(input) input.value = name } else { input.parentNode.removeChild(input); } })
 <input type="checkbox" id="checkBox"> <script src="script.js"></script>

We could implement this as follows under the assumption that the name of the credit card is available.在信用卡名称可用的假设下,我们可以如下实现。 Let us call it creditCardName .让我们称之为creditCardName

My example uses react-native-checkbox and the standard TextInput component.我的示例使用react-native-checkbox和标准TextInput组件。 If you are using a different component, then the overall pattern still stays the same.如果您使用不同的组件,那么整体模式仍然保持不变。

const [isChecked, setChecked] = useState(false);
const [userName, setUserName] = useState('');

return (
    <CheckBox
        value={isChecked}
        onPress={(newVal) => setChecked(newVal)}
     />
     <TextInput
       editable={!isChecked}
       placeholder='Nombre completo'
       value={isChecked ? creditCardName : userName}
       onChangeText={setUserName}
     />
)

The key-parts are as follows:关键部分如下:

  1. Let the TextInput be editable if and only if isChecked is false, that is the CheckBox is unchecked.当且仅当isChecked为 false 时,让TextInput可编辑,即未选中CheckBox
  2. Let value of the TextInput be the creditCardName if the CheckBox is checked and set it to the userName otherwise.如果CheckBox被选中,则将TextInputvalue creditCardName ,否则将其设置为userName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在React native中获取TextInput值和复选框状态 - Get TextInput Value and Checkbox state in React native REACT NATIVE:我如何使用 TextInput 的值进行计算 - REACT NATIVE: How can i make calculation with the Value of TextInput 如何将 TextInput 的值存储到本地存储并在应用程序以本机反应启动时获取它们? - How can I store the value of TextInput to local storage and get them when the app starts in react native? 如何在 react-native 中获取 TextInput 值 - How to get the TextInput value in react-native 如何在 javascript/jquery 中获得选中的复选框标题值? - How can i get a checked Checkbox title value in javascript/jquery? 如果选中复选框,我如何从复选框中获取属性值? - How can i get attribute value from a checkbox if it's checked? REACT NATIVE:如何获取 TextInput 的值和 Picker 的值 - REACT NATIVE: How to get value of TextInput and value of Picker 如何在react-native中获得NavigatorIOS的子组件TextInput的值 - How to get value of child component TextInput of NavigatorIOS in react-native 如何从 TextInput 获取值并使用 Button React Native 进行设置? - How to get value from TextInput and set it with Button React Native? 如何在不使用状态的情况下在 React Native 中获取 TextInput 的值 - How to get the value of TextInput in React Native without using state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM