首页 > 解决方案 > Reactjs:使用javascript删除输入标签值

问题描述

当我单击按钮时,函数createPost工作,我的数据正确地发送到服务器。但输入值不会删除。

//...existing code...
createPost=()=>{
        var url = 'http://127.0.0.1:8000/myapi/createpost/'
        fetch(url,{
            method:'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Token '+this.props.token,    
            },
            body:JSON.stringify({
                'body':this.state.body
            })
        })
        .then((response)=>{
            response.json()
            document.getElementById("asif").value=''
            this.setState({body:''})
            this.props.rerender()
        })
    }
//...code...
<div className="rest container">
                        <p>
                        <input id="asif" onChange={(e)=>{this.setState({body:e.target.value})}}
                        style={{width:'100%', height:'36px', borderRadius:'25px'}}
                        placeholder={"What's on your mind, "+this.props.userdata.firstname+"?"} />
                        </p>
                        
                    </div>
//...code

我在另一个组件中编写了几乎相同的代码并且它可以工作。但是我的代码中有什么问题?

标签: javascripthtmlreactjs

解决方案


推荐阅读