简体   繁体   English

使用Redux表单,如果未填写任何字段,如何禁用提交按钮?

[英]Using Redux Form, how can I disable the submit button if no fields have been filled out?

I'm using Redux Form v.7.1.2 and am trying to have the submit button disabled initially. 我正在使用Redux Form v.7.1.2,并试图首先禁用“提交”按钮。 When the user activates the change or keyup events, I want the form to check if ANY of the form fields within the form have any value. 当用户激活changekeyup事件时,我希望表单检查表单中的任何表单字段是否具有任何值。 If so, I want the submit button to become enabled. 如果是这样,我希望启用提交按钮。 I don't have any fields required. 我没有任何必填字段。 I just need to make sure that at least one of them has a value filled in. 我只需要确保其中至少一个值已填写即可。

I have achieved this in jQuery: 我已经在jQuery中实现了这一点:

// Change disabled attribute on search box submit button depending on if there is a value in any field
  $searchFormFields.on('change keyup', function() {
    $('#search-box-search-btn').prop('disabled', true);
    $searchFormFields.each(function() {
      if ($(this).val().length > 0) {
        $('#search-box-search-btn').prop('disabled', false);
        return false; // break the loop
      }
    });
  });

I am converting the site to use React and Redux now though, and I have absolutely no idea where to begin trying to get this to work. 我现在正在将该站点转换为使用React和Redux,而且我完全不知道从哪里开始尝试使其工作。 Any ideas? 有任何想法吗?

If you just want to check that no fields have been filled out, you can use the following line: 如果您只想检查没有填写的字段,可以使用以下行:

<button type="submit" disabled={this.props.pristine}>Submit</button>

pristine is a prop that redux-form adds that you can reference if the form has not had any fields filled out. pristine是redux-form所添加的一项道具,如果表单中未填写任何字段,您可以引用该道具。 You can check out more in their API Docs . 您可以在他们的API文档中查看更多信息。 It is comparing the current form values to the initial values on the form. 它将当前表单值与表单上的初始值进行比较。

It turns out I actually needed to disable the button if the form had not been changed since the last submit . 事实证明,如果自上次提交以来未更改表单,则实际上我需要禁用该按钮。

In order to check for that condition, you can assign the current values as the initial values when submitting the form. 为了检查这种情况,您可以在提交表单时将当前值指定为初始值。 Then when determing if a form is pristine , redux-form will compare any current values to the last initial values that were set on the latest submit. 然后,当确定表单是否pristine ,redux-form会将当前值与最近提交时设置的最后初始值进行比较。

import { Field, reduxForm } from 'redux-form';

class SearchFilters extends Component {
  constructor(props) {
    super(props);
    this.onSubmit = this.onSubmit.bind(this);
  }

  onSubmit(values) {
    // Check if the form values have changed since the last submit
    // The dirty boolean (signifies if the form has changed from its initial values) is established by redux-form by comparing the currently entered values to the initial form values
    // After we identify the form as having been changed, we will send the new form values to replace the original initial form values so redux-form is always comparing the current form values against the last submitted form values
    if (this.props.dirty) {
      // Set the initial values for the form on the redux store (This is used by redux form to compare the current values to the initial values so we can determine if there are new values since the last submit)
      this.props.initialize(values);
    }
  }

  renderField(field) {
    return (
      <div className="search-parameter">
        <input type="text" {...field.input} />
      </div>
    );
  }

  render() {
    const { handleSubmit, submitting, invalid } = this.props;

    return (
      <form onSubmit={handleSubmit(this.onSubmit)} >
        <Field
          component={this.renderField}
          key="keyword"
          name="keyword"
        />
        <Field
          component={this.renderField}
          key="type"
          name="type"
        />
        <button
          type="submit"
          disabled={submitting || pristine || invalid}
        >
          Search
        </button>
      </form>
    );
  }
}

export default reduxForm({
  validate,
  form: 'SearchFiltersForm',
})(connect(mapStateToProps, null)(SearchFilters));

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

相关问题 在使用 jquery 填写表单之前,如何禁用提交表单按钮? - How can I disable submit form button until form is filled using jquery? 如果未填写表单,则禁用提交按钮 - Disable submit button if form not filled out 禁用表单提交,直到使用jQuery验证字段 - Disable Form Submit until Fields have been validated using jQuery 如何检查表单中所有必填字段的填写时间? - How can I check when all of the required fields of a form has been filled out? 禁用/启用提交按钮,直到填写完所有表格 - Disable/Enable Submit Button until all forms have been filled 我如何使用jQuery禁用表单的“提交”按钮,直到每个必填字段都已填写? - How do I use jQuery to disable a form's submit button until every required field has been filled? 未填写必填字段时停止提交表单 - Stop form submission when the required fields have not been filled out 禁用提交按钮,直到表单填写完毕JQuery / JavaScript - Disable Submit Button Until Form Filled Out JQuery/JavaScript 禁用表单提交按钮,直到填充所有输入/文本区域字段,而不使用 for 循环(无 jQuery) - Disable form submit button until all input/textarea fields are filled, without using for loop (no jQuery) 在 ReactJS 中填写所有输入字段之前,如何禁用表单提交按钮? - How to disable form submit button until all input fields are filled in ReactJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM