Determine which checkbox is currently selected in Vue and send corresponding data based on the checked status

A radio form that is conditionally disabled by another checkbox. One radio button can send either true or false and the other has an input that sends some data.

How can I determine which radio button is checked and decide whether to send true/false or the data?


  <input type="radio" id="one"
  value="Voicemail"
  v-bind:disabled="!checked"
  v-model="checkedValue">
  <label for="one">Voicemail</label>

  <input type="radio" id="two"
  value="Destination"
  v-bind:disabled="!checked"
  v-model="checkedValue">

  <label for="two">Destination</label>
  <input type="text" v-model="destinationNumber" v-bind:disabled="!checked">
  <button class="button" type="submit" name="button" v-bind:disabled="!checked">Submit</button>

data () {
  return {
      checked: '',
      checkedValue: '',
      destinationNumber: ''
  }
},
methods: {
  handleExtensionFormSubmit () {
    const postData = {
      destination: this.checkedValue === 'Destination' ? this.destinationNumber : '',
      voicemail: this.checkedValue === 'Voicemail' ? true : false
    }

this.$http.put(/someUrl)

The request should be formatted like this:

destination: 666,
voicemail: false

Or

destination: '',
voicemail: true

Any assistance would be greatly appreciated.

Answer №1

Your code is currently experiencing a significant issue due to the post object you are using.

postData = {
  destination: this.checkedValue == 'Destination' ? this.destinationNumber : '',
  voicemail: this.checkedValue == 'Voicemail' ? true : false
}

The expression

this.checkedValue = 'Destination'
is not performing a check, but rather setting the value of checkedValue to 'Destination', which always results in true. To address this, both instances should be changed to ==. Moreover, if you intend to return false for Voicemail, you should use ?true:false instead of ?true:''. Although I may not fully grasp the purpose of your code, these modifications should guide you in the right direction.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Implementing React to dynamically load and update value in component, enabling user interaction with the

As a newcomer to the world of React (16.4.2), I am trying to grasp its functionality without delving into Redux just yet; my focus is solely on understanding the core React library. In my application, there is an input component called RangeInput, which r ...

Retrieving multiple images from a directory and storing the image filenames in a JSON array

Currently, I am attempting to locate and retrieve the images stored within a specific folder using the following code. This code successfully retrieves the image names along with the total count of images. Subsequently, my goal is to save this information ...

What's the best way to keep track of the number of objects I've created

Using this HTML code, I can create member objects. However, I also need to determine the count of these member objects for certain calculations. Additionally, when a member object is deleted, the count of member objects should be reduced accordingly. The ...

Attaching a function to the "onselect" event that manages the character limit within an input text field

I have been developing a function to restrict the number of characters a user can enter into a text field. Here's what I have so far: $.fn.restringLength = function (id, maxLen) { $(id).keypress(function(e){ var kCode = ...

The angular view is lacking a CSS class

index.html: <div ng-view="myview.html"></div> myview.html: <table class="table table-striped table-hover"> <tr> <td>Name</td> <td>Product</td> </tr> <tr ng-repeat="deal in deals" class="clickableR ...

Tips for sending a pre-made Vue component as a prop in Vue.js

Is there a way to pass an instantiated Vue Component as a prop to another component and then render it, similar to how it's done in React: <Button :icon=<IconComponent size="25" /> :link="http://wikipedia.org">Click her ...

What steps do I need to take to ensure my TypeScript module in npm can be easily used in a

I recently developed a module that captures keypressed input on a document to detect events from a physical barcode reader acting as a keyboard. You can find the source code here: https://github.com/tii-bruno/physical-barcode-reader-observer The npm mod ...

Animate the React Bootstrap modal only when it is shown or hidden

I am experimenting with the idea of transitioning smoothly from one modal to another in bootstrap Modals for react (using react-bootstrap). However, I am facing challenges due to the fade CSS class causing flickers and unwanted animations. My goal is to h ...

The presence of the !doctype html tag completely messes up my

I am currently working on a code snippet that is supposed to provide the screen coordinates of a given object: <!DOCTYPE HTML> <html> <head> </head> <body style="margin:0px;padding:0px;"> <div id="help" style="top:100px;r ...

Guide on creating and synchronizing an XML/JSON file that stores beat information (BPM) for audio using JavaScript

Looking to sync Javascript events with the BPM of music for a game similar to "Guitar Hero." We start with: In order to create a track file based on beat detection (with each BPM saved like sheet music), how can it be generated beforehand rather than in ...

An error was encountered while linting /app/layout.tsx at line 16: Rule "@typescript-eslint/no-empty-function" was violated due to inability to read properties of undefined (reading 'getTokens')

I am puzzled as to why the function that generates JSX is being checked by the "next lint" script with the rule "@typescript-eslint/no-empty-function". The code snippet at line 16 of the layout.tsx file looks like this: export default function RootLayout( ...

Errors encountered while starting Angular due to issues in package.json configuration

Summary: Encountered an error while using 'Angular' for the first time, indicating tsc was not found in the package.json file. Details: As a beginner with Angular, I followed an example from a book and attempted to start it with np ...

To restore the position of the chosen object in Three.js after clicking reset

I am facing an issue with resetting the position of my latest Object in three.js. Initially, my code consists of the following: function onDocumentMouseDown( event ) { event.preventDefault(); var vector = new THREE.Vector3( mouse ...

Add a sub-modal window inside a separate container

Is there a way to attach this modal to an external div? The current setup involves nesting it within a container that has a fixed height. I'm looking to move it outside of that container. Here is the JavaScript code for the modal: client.fetchQuery ...

Guide to decoding JSONP data sent from a remote server

Working on retrieving data using JSONP. After observing the returned data in Firebug, I can confirm that it is being returned correctly. However, I am facing a challenge in figuring out how to parse it. Is the data returning as a nested array? The callback ...

How to retrieve data attribute from a different attribute in VueJS

I'm working with code that utilizes the VueGoodTable component, and I'm looking for a way to allow users to dynamically change the unit of the amount displayed: <template> <div class="container"> <h1>Welcome to t ...

Utilizing Vue: Invoking a Function from a Separate Component

I am attempting to trigger a function from another component within my Vue application. Within my library.vue file, there is a function designed to trigger a popup with a specific id: displayPopup(id: String) In my view, I have a route parameter containi ...

Has the Soundcloud web widget API become obsolete?

I am currently working with the SoundCloud widget API (). My main goal is to retrieve information about the tracks within a set. Unfortunately, it seems like I am only able to utilize the methods widget.getSounds() and widget.getCurrentSound successfully ...

A comprehensive guide on integrating the BarcodeScanner plugin in your NativeScript-vue application

I'm just starting out with NativeScript and I'm struggling to scan a QR code. I attempted to use the nativescript-barcodescanner plug-in with Vue.js, but encountered errors when running all the demos. Despite searching for examples, I couldn&apos ...

Is there a way to store the JWT response header retrieved with fetch?

I am currently utilizing React and employing fetch to send a request to the server: fetch("http://localhost:8001/api/login", { method: 'post', headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" }, ...