dimanche 4 mars 2018

HTML Checkbox - save value as "Yes" / "No" instead of "true" / "false" in database (Javascript)

I want to save the checkbox value as "Yes" / "No" instead of "true" / "false" in database. Am using reactjs to fetching the values and storing in the database.

Please find my below code, that am trying.

handleInputChange(e) {
    const target = e.target;
    const name = target.name;
    const item = this.state.item;
    const value = target.type === 'checkbox' ? target.checked : target.value;
    if( name === "should_create" && value === false )
        {
            this.state.item.should_create="No";
        }
    else if( name === "should_create" && value === true )
        {
            this.state.item.should_create=="Yes";

        }
    else
        {
            this.state.item.should_create=="";
        }
            item[name] = value;
            this.setState({ item: item });

    }

HTML Check box code :

<input name="should_create" checked={this.state.item.should_create==true ? "true":''} type="checkbox" value={ this.state.item.should_create } onChange={ this.handleInputChange } />

But, the above code is not working properly. Its storing the default value "true" / "false" only. How can i change it to "Yes" / "No" value ?

I have multiple "Checkbox" component, but i want this "Yes" / "No" options for this only one component. Remaining component should be as default.




Aucun commentaire:

Enregistrer un commentaire