简体   繁体   English

如果React JS中的输入不为空,请选择另一个选项

[英]Select another option if input not empty in react js

I have some code in my react component: 我的React组件中有一些代码:

<legend>Data:</legend>
          <Input type="text" name="Payment" id="Payment" className="validate[required, custom[integer], max[9999999999999]]" placeholder="2000" addonBefore="Payment" addonAfter="cents" />
          <Input type="text" name="Value" id="Value" className="validate[required, custom[integer], max[9999999999999]]" placeholder="3500" addonBefore="Value" addonAfter="cents" />
          <Input type="select" label="Some code" placeholder="placeholder" >
            <option value="0">option1</option>
            <option value="1">option2</option>
            <option value="2">option3</option>
          </Input>
          <Input type="select" label="Some category" placeholder="Category">
            <option value="0">category1</option>
            <option value="1">category2</option>
            <option value="2">category3</option>
            <option value="3">Category4</option>
            <option value="4">Category5</option>
          </Input>
          <Input type="select" label="Another code" placeholder="some code">
            <option value="1">code1</option>
            <option value="2">code2</option>
            <option value="3">code3</option>
            <option value="4">code4</option>
            <option value="5">code5</option>
            <option value="8">code6</option>
            <option value="7">code7</option>
          </Input>
      </fieldset>

How can i change select option for example in "Some category", if input payment not empty? 如果输入的付款不为空,如何更改“某些类别”中的选择选项? I mean, if user enter some text in "Payment" input(for example), select option must be changed from default to second or third. 我的意思是,如果用户在“付款”输入中输入一些文本(例如),则选择选项必须从默认更改为第二或第三。 And something like that with another text input and select option? 还有其他带有文本输入和选择选项的东西吗? How can I do this? 我怎样才能做到这一点?

You have to make all your inputs - controlled 您必须进行所有输入- 受控
Save input values to a state of your component and based on those values perform the logic you need to render different options 将输入值保存到组件的state ,然后根据这些值执行呈现不同options所需的逻辑

  <select value={this.state.myValue} onChange={this.handleChange}>
    <option value="A">Apple</option>
    <option value="B">Banana</option>
    <option value="C">Cranberry</option>
  </select>

You need to use value attribute instead of defaultValue to make your select - controlled 您需要使用value属性而不是defaultValue来进行选择- 受控
Read more in docs 文档中阅读更多内容

Well, I came to this code(it's worked for me and solve my problem): 好吧,我来到了这段代码(它为我工作并解决了我的问题):

state : {
  text: '',
  select: 0
},
handleChange({target: {id, value}}) {
  const data = {[id]: value};
  if (id == 'Payment' && value.length != 0) {
    data.select = 4;
  } else if (id == 'Value' && value.length != 0){
    data.select = 2;
  } else {
    data.select = 0;
  }
  this.setState(data);
},
/* some code in render fucntion for our form*/ 

Maybe it will be helpfull for someone. 也许对某人会有帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM