简体   繁体   English

我在一个组件中同时使用两个按钮,当我单击它而不是另一个按钮时,我只希望一个按钮起作用

[英]i am using two buttons simultaneously in one component, i want only one button to work when i click it not the other button

I am currently new at ReactJs.我目前是 ReactJs 的新手。 So, i have implemented two buttons in one container and i dont know how to make each one of them work separately.所以,我在一个容器中实现了两个按钮,但我不知道如何让它们分别工作。 If i am clicking the first button then even the second button is executing.如果我单击第一个按钮,那么即使是第二个按钮也在执行。 Can anyone suggest what changes can I make in my code to make each button work separately.任何人都可以建议我可以在我的代码中进行哪些更改以使每个按钮单独工作。 So, now i have updated the code, As所以,现在我已经更新了代码,因为

import { Select, Form, Input, Button, DatePicker, Icon } from 'antd';
import './CreatePrescription.scss';
import { Helmet } from 'react-helmet';

const { Option } = Select;
const { TextArea } = Input;

class CreatePrescriptionFormData extends React.Component {
  state = {
    formType: 'add-patient',
    prescriptionItems: [],
  };

  handleSubmit = e => {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        console.log('Received values of form: ', values);
      }

      // Should format date value before submit.
      const fieldsValues = {
        ...values,
        'date-picker': values['date-picker'].format('DD-MM-YYYY'),
      };
      console.log('Received values of form: ', fieldsValues);
    });
  };

  addprescriptionItem() {
    this.setState({ prescriptionItems: [...this.state.prescriptionItems, ''] });
    console.log({ prescriptionItems: [...this.state.prescriptionItems, ''] });
  }

  handleChange(e, index) {
    this.state.prescriptionItems[index] = e.target.value;

    // set the changed state
    this.setState({ prescriptionItems: this.state.prescriptionItems });
  }

  handleRemove(index) {
    // remove ana item at the index
    this.state.prescriptionItems.splice(index, 1);

    console.log(this.state.prescriptionItems, '$$$$');

    // update the state
    this.setState({ prescriptionItems: this.state.prescriptionItems });
  }
  addprescriptionItems() {
    this.setState({ prescriptionItems: [...this.state.prescriptionItems, ''] });
    console.log({ prescriptionItems: [...this.state.prescriptionItems, ''] });
  }

  handleChange(e, index) {
    this.state.prescriptionItems[index] = e.target.value;

    // set the changed state
    this.setState({ prescriptionItems: this.state.prescriptionItems });
  }

  handleRemove(index) {
    // remove ana item at the index
    this.state.prescriptionItems.splice(index, 1);

    console.log(this.state.prescriptionItems, '$$$$');

    // update the state
    this.setState({ prescriptionItems: this.state.prescriptionItems });
  }

  render() {
    const { getFieldDecorator } = this.props.form;
    const tailFormItemLayout = {
      wrapperCol: {
        xs: {
          span: 24,
          offset: 0,
        },
        sm: {
          span: 16,
          offset: 4,
        },
      },
    };
    return (
      <div className="comp-wrap">
        <Helmet
          titleTemplate="%s - Sample-App"
          defaultTitle="Create Prescription | Sample-App"
        >
          <meta name="description" content="sample application" />
        </Helmet>

        <div className="component-title">
          <div className="container">
            <h1>Add Prescription</h1>
          </div>
        </div>
        <div className="container">
          <div className="cards-wrap row justify-content-md-center">
            <div className="col-md-12 col-sm-12 col-xs-12">
              <div className="prescription-form">
                <div className="login-form" style={{ borderRadius: '5px' }}>
                  <div className="pr-login-box">
                    <Form onSubmit={this.handleSubmit}>
                      <div>
                        <Form.Item label="Start" className="prescription_date">
                          {getFieldDecorator('start', {
                            rules: [
                              {
                                type: 'object',
                                required: true,
                                message: 'Please select date!',
                              },
                            ],
                          })(<DatePicker />)}
                        </Form.Item>
                        <Form.Item label="End" className="prescription_date">
                          {getFieldDecorator('end', {
                            rules: [
                              {
                                type: 'object',
                                required: true,
                                message: 'Please select date!',
                              },
                            ],
                          })(<DatePicker />)}
                        </Form.Item>
                      </div>
                      {this.state.prescriptionItems.map((item, index) => {
                        return (
                          <div className="new-item" key={index}>
                            <Form.Item
                              label="What"
                              className="prescription_date field_width"
                            >
                              {getFieldDecorator('what', {
                                rules: [
                                  // {
                                  //     required: true,
                                  //     message: 'Please select Medicine!',
                                  // },
                                ],
                              })(
                                <Select placeholder="Select a option">
                                  <Option value="">Abhayarishtam</Option>
                                  <Option value="">Amritarishtam</Option>
                                  <Option value="">Ashtachurnam</Option>
                                  <Option value="">Brahmi Ghritam</Option>
                                  <Option value="">
                                    Amritotharam Kashayam
                                  </Option>
                                </Select>,
                              )}
                            </Form.Item>

                            <Form.Item
                              label="When"
                              className="prescription_date field_width"
                            >
                              {getFieldDecorator('when', {
                                rules: [
                                  // {
                                  //     required: true,
                                  //     message: 'Please select!',
                                  // },
                                ],
                              })(
                                <Select placeholder="Select When">
                                  <Option value="morning">Morning</Option>
                                  <Option value="afternoon">Afternoon</Option>
                                  <Option value="evening">Evevning</Option>
                                </Select>,
                              )}
                            </Form.Item>
                            <Form.Item
                              label="Quantity"
                              className="prescription_date field_width"
                              style={{ width: '15%' }}
                            >
                              {getFieldDecorator('quantity', {
                                rules: [
                                  // {
                                  //     required: true,
                                  //     message: 'Please select!',
                                  // },
                                ],
                              })(
                                <Select placeholder="Select Quantity">
                                  <Option value="1/2">1/2</Option>
                                  <Option value="1">1</Option>
                                  <Option value="2">2</Option>
                                  <Option value="3">3</Option>
                                </Select>,
                              )}
                            </Form.Item>
                            <Form.Item
                              label="Unit"
                              className="prescription_date field_width"
                              style={{ width: '15%' }}
                            >
                              {getFieldDecorator('unit', {
                                // rules: [
                                //     {
                                //         required: true,
                                //         message: 'Please select!',
                                //     },
                                // ],
                              })(
                                <Select placeholder="Select a option">
                                  <Option value="ltr">ltr</Option>
                                  <Option value="grm">grm</Option>
                                </Select>,
                              )}
                            </Form.Item>
                            <Form.Item
                              label="How Often"
                              className="prescription_date field_width"
                            >
                              {getFieldDecorator('often', {
                                // rules: [
                                //     {
                                //         required: true,
                                //         message: 'Please select!',
                                //     },
                                // ],
                              })(
                                <Select placeholder="Select a option">
                                  <Option value="after_meal">After Meal</Option>
                                  <Option value="before_meal">
                                    Before Meal
                                  </Option>
                                </Select>,
                              )}
                            </Form.Item>
                            <Form.Item>
                              {getFieldDecorator('notes')(
                                <TextArea rows={3} />,
                              )}
                            </Form.Item>
                            {/* <input onChange={(e) => this.handleChange(e, index)} value={item}></input> */}
                            <a
                              className="remove"
                              onClick={() => this.handleRemove(index)}
                            >
                              <Icon type="close-circle" />
                            </a>
                          </div>
                        );
                      })}
                      <Button
                        onClick={e => this.addprescriptionItem(e)}
                        className="btn"
                        type="primary"
                      >
                        Add Prescription Item
                      </Button>

                      <Button
                        onClick={e => this.addprescriptionItems(e)}
                        className="btn"
                        type="primary"
                      >
                        Add Compound Medicine
                      </Button>

                      <Form.Item {...tailFormItemLayout} className="form-btn">
                        <Button
                          className="cl-clone"
                          type="button"
                          htmlType="button"
                        >
                          Cancel
                        </Button>
                        <Button
                          className="cl-fill btn-h-40 btn-save float-right"
                          type="primary"
                          htmlType="submit"
                        >
                          Save
                        </Button>
                      </Form.Item>
                    </Form>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

const CreatePrescription = Form.create({ name: 'coordinated' })(
  CreatePrescriptionFormData,
);

export default CreatePrescription;

addprescriptionItem and addprescriptionItems 

are doing exactly the same stuff.正在做完全相同的事情。 Can u please elaborate more what you are trying to achieve here?你能详细说明你在这里想要实现的目标吗?

I think you will want to do some logic in the functions.我想你会想要在函数中做一些逻辑。 Currently, you are just doing this, which doesn't look like it does much useful:目前,您只是在这样做,这看起来并没有多大用处:

this.setState({ prescription items: [...this.state.prescriptionItems, ''] });

One button is called ' Add Compound Medicine' so I'm guessing you want to add or remove things from the items array, ego something with the data being passed back:一个按钮叫做“添加复合药物”,所以我猜你想从 items 数组中添加或删除一些东西,把数据传回一些东西:

addprescriptionItems(e) {
    console.log(e);
    const item = // whatever
    this.state.prescriptionItems.push(item)
}

暂无
暂无

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

相关问题 当我想要两个按钮时,我的 javascript 函数中只有一个按钮 - Only one button in my javascript function when I want two 我有 3 个按钮,我希望其中一个默认处于单击状态,除非我单击 HTML/JS 中的另一个按钮 - I have 3 buttons, I want one of them to be in clicked state by default unless I click the other button in HTML/JS 当您单击该按钮时,该动作不是我想要的 - When you click on the button, the action is not the one that I want 我想要一个按钮中有两个功能 - I want Two functions in one button 当我尝试单击一个单选按钮时,它将选择另一个 - When I try to click one radio button it selects the other 当我单击主组/按钮阵列中的一个按钮时如何显示一组按钮 - How to display one set of buttons when I click on one button of the main set/array of buttons Reactjs:我正在检查基于 state 的单选按钮,但在选中一个单选按钮时无法单击其他单选按钮 - Reactjs: I am checking radio button based on state but can't click other radio button when one is checked 当我单击 javascript 中的删除按钮时,我想逐行删除 - I want to delete one by one row when i click delete button in javascript 当我单击特定的“addToBasket”按钮时,我不想更改所有“addToBasket”按钮。 只有点击的按钮应该改变 - I do not want all the "addToBasket" buttons to change when i click on a specefic "addToBasket" button. Only the clicked button should change 在使用FBML的FB中单击一个按钮后,禁用所有按钮 - Disable all buttons after I click to one button in FB with FBML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM