Button set to disabled state

Before making an asynchronous request, set the button to the disabled state, wait for the backend response, and then set the button to the enabled state.

1. Use the disabled attribute in JavaScript to implement this function.

const button = document.getElementById('myButton');
button.disabled = true; // 禁用按钮

Then, after the asynchronous request returns the result, you can re-enable the button:

button.disabled = false; // 启用按钮

2. If you are using jQuery, you can use the prop method to set the disabled attribute of the button:

$('#myButton').prop('disabled', true); // 禁用按钮

Then, after the asynchronous request returns the result, you can re-enable the button:

$('#myButton').prop('disabled', false); // 启用按钮

3. If you want to prevent users from clicking the button repeatedly while waiting for the response of an asynchronous request, you can disable the button and add a tag to identify whether the backend response is being waited for.

let isWaitingResponse = false;
const button = document.getElementById('myButton');

button.addEventListener('click', () => {
    
    
  if (isWaitingResponse) {
    
    
    return; // 如果正在等待响应,则不执行任何操作
  }

  isWaitingResponse = true; // 标记为正在等待响应
  button.disabled = true; // 禁用按钮

  // 发送异步请求
  sendAsyncRequest().then(() => {
    
    
    // 请求成功后执行的操作
  }).catch(() => {
    
    
    // 请求失败后执行的操作
  }).finally(() => {
    
    
    isWaitingResponse = false; // 标记为不再等待响应
    button.disabled = false; // 启用按钮
  });
});

In this way, while waiting for the asynchronous request response, when the user clicks the button again, it will be ignored. At the same time, after waiting for the asynchronous request response to end, the button will automatically return to the available state.

4. In Vue, you can disable buttons in the following ways:

  • Define a variable in data to save the status of whether the button is disabled:
data() {
    
    
  return {
    
    
    isButtonDisabled: false
  }
}
  • Bind the disabled attribute of the button to this variable in the template:
<button :disabled="isButtonDisabled" @click="sendRequest">发送请求</button>
  • In the sendRequest method, disable the button and send an asynchronous request
methods: {
    
    
  async sendRequest() {
    
    
    this.isButtonDisabled = true; // 禁用按钮

    try {
    
    
      // 发送异步请求
      const response = await axios.post('/api/someEndpoint', {
    
     data: 'someData' });
      console.log(response);
    } catch (error) {
    
    
      console.error(error);
    } finally {
    
    
      this.isButtonDisabled = false; // 启用按钮
    }
  }
}

This way, when an asynchronous request is made, the button is disabled and will not be re-enabled until the request is completed and responded to.

Use the axios library to initiate a GET request and handle the success and failure callback functions through the .then method. When the request is successful, the data returned by the server is assigned to the data variable and the log is output; when the request fails, the error message is output to the console.

// 定义一个变量用于保存请求的数据
let data = null;

// 发起请求
axios.get('/api/taskList')
  .then(response => {
    
    
    // 请求成功,将返回的数据赋值给 data 变量
    data = response.data;
    console.log('请求成功', data);
  })
  .catch(error => {
    
    
    // 请求失败,打印错误信息
    console.error('请求失败', error);
  });

Guess you like

Origin blog.csdn.net/weixin_57541715/article/details/130156946