【问题标题】:Cannot click inside text input无法在文本输入内部单击
【发布时间】:2019-11-18 12:00:39
【问题描述】:

我有一个包含几个字段的基本表单。但是,我无法在悬停时单击项目名称输入字段。我可以通过点击标签来专注于它,但点击不起作用。这是 CSS 对齐问题吗?我究竟做错了什么?请帮忙!

这是表单域的截图:

这里是沙盒中的sn-p代码:

https://codesandbox.io/s/trusting-frost-gp7pj?from-embed

onFocus 能解决这个问题吗?

这里是sn-p的代码:

import React, { Component } from "react";
import Dropdown from "react-dropdown";
import "react-dropdown/style.css";

import styled from "styled-components";

const InputText = styled.input.attrs({
  className: "form-control"
})``;

const RaiseMRNForm = styled.div.attrs({})`
  background-color: papayawhip;
  width: 100%;

  height: auto;
`;

const ModalWrapper = styled.div.attrs({})`
  border: 1px solid #d0cccc;
  box-shadow: 0 5px 8px 0 rgba(0, 0, 0, 0.2), 0 7px 20px 0 rgba(0, 0, 0, 0.17);
  transition: all 0.8s;
  height: 400%;
`;

const ModalHeader = styled.div.attrs({})`
  background-color: #035385;

  width: 100%;
  display: inline-block;
`;

const MRNHeading = styled.div.attrs({})`
  width: 50%;
  float: left;
  margin-top: 1%;
  color: white;
  margin-left: 1%;
`;

const CrossButton = styled.div.attrs({})`
  color: white;
  width: 5%;
  float: right;
`;

const InputTextAreas = styled.div.attrs({})`
  background-color: gray;
  display: inline-block;
  width: 100%;
`;

const InputClient = styled.div.attrs({})`
  width: 40%;
  margin-top: 1%;
  margin-left: 5%;
  float: left;
  position: absolute;
`;

const InputStockItem = styled.div.attrs({})`
  width: 40%;
  float: left;

  margin-top: 1%;
  position: absolute;
  margin-left: 50%;
`;

const StockInformation = styled.div.attrs({})`
  height: 100%;
  width: 100%;
  position: absolute;

  margin-top: 7%;
`;

const DropDownContainer = styled.div.attrs({})`
  margin-left: 5%;
  width: 40%;
  position: absolute;
`;

const InputStockQuantity = styled.div.attrs({})`
  margin-left: 50%;
  margin-top: 0.2%;
  width: 20%;
  position: absolute;
`;

const InputStockUnit = styled.div.attrs({})`
  margin-left: 5%;
  margin-top: 1%;
  width: 10%;
  position: absolute;
`;

const ModalFooter = styled.div.attrs({})`
  position: absolute;
  background-color: #035385;
  width: 100%;
  height: auto;
`;

const AddButton = styled.button.attrs({})`
  cursor: pointer;
  background: transparent;
  font-size: 16px;
  border-radius: 3px;
  color: white;
  border: 2px solid white;
  margin-top: 1%;
  margin-left: 1%;
  margin-bottom: 1%;
  height: 6%;
  padding: 0.25em 1em;
  transition: 0.5s all ease-out;
  &:hover {
    background-color: palevioletred;
    color: white;
  }
`;

const CancelButton = styled.button.attrs({})`
  cursor: pointer;
  background: transparent;
  font-size: 16px;
  border-radius: 3px;
  color: white;
  border: 2px solid white;
  margin-top: 1%;
  margin-left: 80%;
  margin-bottom: 1%;
  height: 5%;
  padding: 0.25em 1em;
  transition: 0.5s all ease-out;
  &:hover {
    background-color: palevioletred;
    color: white;
  }
`;

const options = ["Safety", "Consumable", "Machinery", "Hardware"];

class MRNmodal extends Component {
  constructor(props) {
    super(props);

    this.state = {
      show: true,
      close: false,

      value: "Safety"
    };
  }

  handleChangeInputClientName = async event => {};

  handleChangeInputStockItemName = async event => {
    const itemName = event.target.value;
    this.setState({ itemName });

    console.log("Inside handleChangeInputStockItemName ========> ", itemName);
  };

  handleChangeInputStockItemQuantity = async event => {
    const itemQuantity = event.target.value;
    this.setState({ itemQuantity });

    console.log(
      "Inside handleChangeInputStockItemQuanity ========> ",
      itemQuantity
    );
  };

  handleChangeInputStockItemUnit = async event => {
    const itemUnit = event.target.value;
    this.setState({ itemUnit });

    console.log("Inside handleChangeInputStockItemUnit ========> ", itemUnit);
  };

  handleDropdownChange = option => {
    console.log("Inside handleDropdownChange ", option);
    this.setState({ itemType: option.label });
    console.log("this.state.itemType", this.state.itemType);
  };

  render() {
    var {
      clientName,
      itemName,
      itemQuantity,
      itemUnit,

      show,
      close
    } = this.state;

    show = true;
    close = false;

    return (
      <div>
        <ModalWrapper
          style={{
            transform: show ? "translateY(0vh)" : "translateY(-100vh)",
            opacity: show ? "1" : "0"
          }}
        >
          <ModalHeader>
            <MRNHeading>
              <h3>Raise MRN</h3>
            </MRNHeading>
            <CrossButton>
              <span>
                <h1>x</h1>
              </span>
            </CrossButton>
          </ModalHeader>

          <RaiseMRNForm>
            <InputTextAreas>
              <InputClient>
                <p style={{ textAlign: "left" }}>Enter Client Name</p>

                <InputText
                  style={{ border: "1px solid black" }}
                  type="text"
                  value={clientName}
                  onChange={this.handleChangeInputClientName}
                />
              </InputClient>

              <InputStockItem>
                <p style={{ textAlign: "left" }}>Enter Stock Item Name</p>

                <InputText
                  style={{ border: "1px solid black" }}
                  type="text"
                  value={itemName}
                  onChange={this.handleChangeInputStockItemName}
                />
              </InputStockItem>
            </InputTextAreas>

            <StockInformation>
              <DropDownContainer>
                <p style={{ textAlign: "left" }}>Enter Stock Type</p>
                <Dropdown
                  value={this.state.itemType}
                  options={options}
                  onChange={this.handleDropdownChange.bind(this)}
                  placeholder="Select an option"
                />
              </DropDownContainer>

              <p style={{ textAlign: "left", marginLeft: "50%" }}>
                Enter Stock Quantity
              </p>
              <InputStockQuantity>
                <InputText
                  style={{ border: "1px solid black" }}
                  type="text"
                  value={itemQuantity}
                  onChange={this.handleChangeInputStockItemQuantity}
                />
              </InputStockQuantity>

              <InputStockUnit>
                <p
                  style={{
                    textAlign: "left",
                    marginLeft: "0%",
                    marginTop: "30%"
                  }}
                >
                  Enter Stock Unit
                </p>
                <InputText
                  style={{ border: "1px solid black" }}
                  type="text"
                  value={itemUnit}
                  onChange={this.handleChangeInputStockItemUnit}
                />
              </InputStockUnit>
            </StockInformation>


          </RaiseMRNForm>

          <ModalFooter>
            <CancelButton>CLOSE</CancelButton>
            <AddButton onClick={this.handleIncludeClient}>CONTINUE</AddButton>
          </ModalFooter>
        </ModalWrapper>
      </div>
    );
  }
}

export default MRNmodal;

我包含了大部分代码,因为我不知道这是否是 CSS 问题。

【问题讨论】:

  • 输入的id必须与标签的for属性相同。编辑:刚刚查看了示例,您使用&lt;p&gt; 作为输入标签。您必须使用&lt;label&gt;。
  • 无法重现错误,按预期工作。
  • 我们无法从您的网址中找到问题。你能在 codepen 或 fiddle 中分享这个吗?
  • 寻求代码帮助的问题必须包括在问题本身中重现它所需的最短代码,最好是在堆栈片段中。尽管您提供了一个链接,但如果它变得无效,那么您的问题对于未来遇到同样问题的其他 SO 用户将毫无价值。见Something in my website OR off-site example doesn't work can I just paste a link。

标签: javascript html css reactjs


【解决方案1】:

使用 HTML &lt;label&gt; 元素而不是 &lt;p&gt; 已经解决了第一个问题。通过这样做,您将能够选择输入字段。

通过使用该标签上的htmlFor 属性以及相应输入上的ID,您还可以单击标签本身来定位输入字段。

htmlFor 是 React 使用 HTML for attribute 的方式。

简短示例 sn-p:

<InputClient>
  <label
    htmlFor="inputClientName" // this targets the input
    style={{ textAlign: "left" }}
  >
    Enter Client Name
  </label>
  <InputText
    id="inputClientName" // consistent id (make it unique)
    style={{ border: "1px solid black" }}
    type="text"
    value={clientName}
    onChange={this.handleChangeInputClientName}
  />
</InputClient>

工作示例: https://codesandbox.io/s/muddy-tree-fb0fd

【讨论】:

  • 您好,雷米,非常感谢您的回复。对此,我真的非常感激。我进行了您建议的更改,并且当我将鼠标悬停在文本框上时能够看到光标。但是它仍然不可点击。有什么想法吗?
  • @sublime_archon 这很奇怪。你 。试过我提供的codeandbox吗? (“工作示例”后面的链接:)您可以点击这些输入字段吗?
  • 我建议删除“绝对位置”定义。而是在每个标签和输入对周围添加&lt;div&gt;。这使得响应式更容易创建。我将在代码框中添加一个示例。
  • 更新了代码框。样式可能已关闭,因为元素现在放置得有点随机。但这就是您要开始寻找的方向。绝对位置与浮动 div 组合(= 相对位置)将不起作用。
猜你喜欢
  • 2011-07-16
  • 1970-01-01
  • 2014-12-13
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
  • 2017-08-24
相关资源
最近更新 更多