Pass single value within onClick (without using a form submit)

前端 未结 2 1284
谎友^
谎友^ 2021-01-29 08:51

Rather than creating several putPriority fetches, I\'d ideally just like to pass a singular value.

But this value won\'t be entered via the user (ie not an input field)<

相关标签:
2条回答
  • 2021-01-29 08:52

    you can use this to get current value :

    putPriority: function(e) {
     e.preventDefault();
     var currentStatus = e.currentTarget.getAttribute("value");
     return fetch(DATA_API, {
     method: 'PUT',
     body: JSON.stringify({
         status: currentStatus,
         tag: [
           {}
          ]
      })
      })
      .then(function(res){ return res.json(); })
      .then(function(data){ alert( JSON.stringify( data ) ) })
    },
    

    this will solve your problem

    var currentStatus = e.currentTarget.getAttribute("value");
    
    0 讨论(0)
  • 2021-01-29 09:02

    You can use bind() to pass value to the handleClick function and the instead of this.state.level just use value liek

    body: JSON.stringify({
         status:value,
         tag:[
           {}
          ]
      })
    

    var App = React.createClass ({
    putPriority: function(value, e) {
        e.preventDefault();
        console.log(value);
      },
    
      render: function(){
            return (
              <div>
        <button onClick={this.putPriority.bind(this, '1')} title="level" value="1">High </button>
    
        <button onClick={this.putPriority.bind(this, "2")} title="level" value="2">Medium </button>
    
        <button onClick={this.putPriority.bind(this, "3")} title="level" value="3">Low </button>
    </div>  
    )  
    }
      
    })
    ReactDOM.render(<App/>, document.getElementById('app'));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    <div id="app"></div>

    0 讨论(0)
提交回复
热议问题