如何获取日期输入的值并将其保存到全局变量?

时间:2019-09-09 19:13:40

标签: javascript

我获得了日期输入字段的值,但是现在我想将该值放入一个全局变量中。我该怎么办?

const props = {
  googleMapURL: `https://maps.googleapis.com/maps/api/js?key=${API_KEY}&v=3.exp&libraries=geometry,drawing,places`,
  containerElement: <div style={{ height: '100%' }} />,
  loadingElement: <div style={{ height: '100%' }} />,
  mapElement: <div style={{ height: '100%' }} />,

  defaultZoom: 5,

  defaultCenter: { location: { lat: 0, lng: 0 } },

  iconPrefix: '/',
  points: points.map(p => createMarker(p)), // this is just for styling the markers.
};

describe('Map component', () => {
  const component = shallow(<Map {...props} />);

  it('It should match snapshot', () => {
    console.log('....: ', component.html()); // -> returns <div style="height:100%"></div>
    expect(component.html()).toMatchSnapshot();
  });
});

1 个答案:

答案 0 :(得分:0)

您具有全局newSelectedDate变量。您应该使用该变量而不是selectedDate

现在请记住,newSelectedDate最初是undefined,因此立即对其进行记录不会返回任何值。只有在change事件发生之后,newSelectedDate才会有值。

let newSelectedDate; // undefined

document.getElementById("datepicker").addEventListener("change", function() {
  newSelectedDate = this.value; // Update newSelectedDate value.
  console.log(newSelectedDate) // Now has a string.
});