React单选按钮仅在未选中时才触发onChange

时间:2019-01-11 17:27:39

标签: javascript html reactjs radio-button

我目前在React.js中的单选按钮有一些问题。由于某些原因,刚刚未被选择的按钮会触发onChange事件,而被选择的按钮则什么也不做。我目前只能获取先前选择的单选按钮的值。

我正在处理App.js中的change事件,并将其作为道具传递给RadioButtons.jsx

内部App.js:

<div className="radio row mx-1 my-0">
  <label>
      <input
          type="radio"
          name="timeframe"
          value="medium_term"
          onChange={this.props.onChange}
          checked={this.props.selectedOption === "medium_term"}
          className="form-check-input"
      />
      the last 6 months
  </label>
</div>

示例:我的单选按钮之一

image_id       filename         thumb 
=============  ============     ================= 
1              image_XYZ.jpg    image_XYZ_thumb.jpg
2              emoticon.png     emoticon_thumb.jpg
3              runner_bean.jpg  runner_bean_thumb.jpg

meta_id  meta_key     meta_value      image_id
=======  ========     ==========      ========
1        filetype     jpg             1
2        keyword      runner          1
3        height       600             1
4        filetype     png             2
5        filesize     5198413         2
6        description  smiley face     2
7        filetype     jpg             3
8        filesize     12485           3
9        description  runnerbean      3
10       keyword      runner          3
11       keyword      vegetable       3

1 个答案:

答案 0 :(得分:0)

Traceback (most recent call last): File "/home/vagrant/venv/ias_lab/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/home/vagrant/venv/ias_lab/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response response = self.process_exception_by_middleware(e, request) File "/home/vagrant/venv/ias_lab/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/vagrant/venv/ias_lab/lib/python3.7/site-packages/django/views/generic/base.py", line 68, in view return self.dispatch(request, *args, **kwargs) File "/home/vagrant/venv/ias_lab/lib/python3.7/site-packages/django/utils/decorators.py", line 45, in _wrapper return bound_method(*args, **kwargs) File "/home/vagrant/venv/ias_lab/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/home/vagrant/venv/ias_lab/lib/python3.7/site-packages/braces/views/_forms.py", line 24, in dispatch return super(CsrfExemptMixin, self).dispatch(*args, **kwargs) File "/home/vagrant/venv/ias_lab/lib/python3.7/site-packages/django/views/generic/base.py", line 88, in dispatch return handler(request, *args, **kwargs) File "./pubsub/views.py", line 141, in post patient_case.status = status, File "/home/vagrant/venv/ias_lab/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py", line 210, in __set__ self.field.remote_field.model._meta.object_name, ValueError: Cannot assign "(<CaseStatus: CaseStatus object (2)>,)": "PatientCase.status" must be a "CaseStatus" instance. 是异步的,因此,如果您尝试以控制台方式记录日志状态,它将返回先前的状态。

如果您要在设置状态后立即setState新设置的状态,则需要在console.log回调中执行以下操作:

setState()