English 中文(简体)
我如何根据选择前一个表格选择更新一个表格的内容?
原标题:How do I update the contents of one Form Select based on the selection of a preceding Form Select?

就我的法典而言,我正试图根据选择的路线更新选定的程序清单。 我根据课程变动找到了相关教授,但无法说明如何迫使候选人名单根据课程选择更新。 任何帮助都受到高度赞赏!

import { Modal, Button, Form, Row, Col } from "react-bootstrap";
import { useState } from "react";

export default function CreateReview(props) {
  const [show, setShow] = useState(false);

  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);

  const yearOptions = [];
  for (let i = 2020; i <= new Date().getFullYear(); i++) {
    yearOptions.push(
      <option key={i} value={i}>
        {i}
      </option>
    );
  }

  var selectedCourseID;

  const handleCourseChange = (e) => {
    selectedCourseID = e.target.value;
    relevantProfessors();
  };

  var relevantProfs = [];

  function relevantProfessors() {
    relevantProfs = [];
    props.profData.map((prof) => {
      if (prof.courseID == selectedCourseID) {
        relevantProfs.push(prof.professor);
      }
    });
    console.log(relevantProfs);
  }

  function submitHandler() {}

  return (
    <>
      <Button variant="primary" onClick={handleShow}>
        Create a Review
      </Button>

      <Modal show={show} onHide={handleClose}>
        <Modal.Header closeButton>
          <Modal.Title>Create New Review</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <Form onSubmit={submitHandler}>
            <Form.Select
              placeholder="selectCourse"
              required={true}
              aria-label="Course Selection"
              onChange={handleCourseChange}
            >
              <option key="blankChoice" hidden value>
                Select a Course
              </option>
              {props.courseData.map((course) => (
                <option key={course.courseID} value={course.courseID}>
                  CIT {course.courseID}: {course.courseName}
                </option>
              ))}
            </Form.Select>
            <Row>
              <Col>
                <Form.Select
                  placeholder="selectSemester"
                  required={true}
                  aria-label="Semester Taken"
                >
                  <option key="blankChoice" hidden value>
                    {" "}
                    Semester{" "}
                  </option>
                  <option>Fall</option>
                  <option>Spring</option>
                  <option>Summer</option>
                </Form.Select>
              </Col>
              <Col>
                <Form.Select
                  placeholder="selectYear"
                  required={true}
                  aria-label="Year Taken"
                >
                  <option key="blankChoice" hidden value>
                    {" "}
                    Year{" "}
                  </option>
                  {yearOptions}
                </Form.Select>
              </Col>
            </Row>
            <br />
            <Form.Select
              placeholder="selectProf"
              required={true}
              aria-label="Professor Selection"
            >
              <option key="blankChoice" hidden value>
                Select a Professor
              </option>
              {relevantProfs.forEach((prof) => {
                console.log(prof);
                return <option>{prof}</option>;
              })}
            </Form.Select>

            <Form.Group className="mb-3" controlId="formBasicPassword">
              <Form.Label>Password</Form.Label>
              <Form.Control type="password" placeholder="Password" />
            </Form.Group>
            <Form.Group className="mb-3" controlId="formBasicCheckbox">
              <Form.Check type="checkbox" label="Check me out" />
            </Form.Group>
            <button>Publish Review</button>
          </Form>
        </Modal.Body>
      </Modal>
    </>
  );
}

我试图补充:

const [show, setShow] = useState(false);
  const [selectedCourseID, setSelectedCourseID] = useState("");
  const [relevantProfs, setRelevantProfs] = useState([]);

  const handleCourseChange = (e) => {
    const courseID = e.target.value;
    setSelectedCourseID(courseID);
    const profs = props.profData.filter((prof) => prof.courseID === courseID);
    setRelevantProfs(profs.map((prof) => prof.professor));
  };

但它与我目前具有同等效力。 谢谢!

问题回答

我得以确定对我问题的答复。 我只是补充说,在选定课程时,使用国是:

  var relevantProfs = [];

  function relevantProfessors() {
    relevantProfs = [];
    props.profData.map((prof) => {
      if (prof.courseID == selectedCourseID) {
        relevantProfs.push(prof.professor);
      }
    });
  }

  const [relatedProfs, setRelatedProfs] = useState(relevantProfs);

摘自相关程序上的每一页,改为:

<Form.Select
            placeholder="selectProf"
            required={true}
            aria-label="Professor Selection"
            options={relatedProfs}
          >
            {relatedProfs.map((prof) => {
              return <option>{prof}</option>;
            })}
          </Form.Select>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签