最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Alpine.js how to watch for changes for radio button - Stack Overflow

programmeradmin3浏览0评论

Suppose I have this html and I want to do something each time user changes the pet:

<input type="radio" id="cat" name="pet" value="cat">
<input type="radio" id="dog" name="pet" value="dog">

I tried this way:

<input type="radio" id="cat" :change="onPetChange('pet', 'cat')" name="pet" value="cat">
<input type="radio" id="dog" :change="onPetChange('pet', 'dog')"  name="pet" value="dog">

But that doesn't work, it just output all values and calls the onPetChange() twice.

Suppose I have this html and I want to do something each time user changes the pet:

<input type="radio" id="cat" name="pet" value="cat">
<input type="radio" id="dog" name="pet" value="dog">

I tried this way:

<input type="radio" id="cat" :change="onPetChange('pet', 'cat')" name="pet" value="cat">
<input type="radio" id="dog" :change="onPetChange('pet', 'dog')"  name="pet" value="dog">

But that doesn't work, it just output all values and calls the onPetChange() twice.

Share Improve this question asked Mar 1, 2021 at 17:30 Garfield LasagaGarfield Lasaga 4181 gold badge6 silver badges14 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

You can x-model the radio value, then watch for changes to the value, and then do something. In this case, just console.log

<div x-data="{pet: ''}" x-init="$watch('pet', value => console.log(value))">
  <input type="radio" id="cat" value="cat" x-model="pet">
  <input type="radio" id="dog" value="dog" x-model="pet">
  <p>Pet: <span x-text="pet"></span></p>
</div>

<script src="https://cdn.jsdelivr/gh/alpinejs/[email protected]/dist/alpine.min.js"></script>
<div x-data="{pet: ''}" x-init="$watch('pet', value => console.log(value))">
  <input type="radio" id="cat" value="cat" x-model="pet">
  <input type="radio" id="dog" value="dog" x-model="pet">
  <p>Pet: <span x-text="pet"></span></p>
</div>

This works perfect:

<script src="https://cdn.jsdelivr/gh/alpinejs/[email protected]/dist/alpine.min.js"></script>
<div x-data="{res: ''}">
<input type="radio" id="cat" @change="console.log(res)" name="pet" value="cat" x-model="res">
<label for="cat">Cat</label><br>
<input type="radio" id="dog" @change="console.log(res)"  name="pet" value="dog"  x-model="res">
<label for="dog">Dog</label><br>
<p x-text="res"></p>
</div>

I think you have a typo on your HTML tag. It should be "onchange" and not ":change".

As you have not provided your onPetChange function, I do not know if it could be also a problem for you. Have you got that function in place?

Anyways here it is a script that prints on your console "hello world" when you select the first radio button.

Try with:

<input type="radio" id="cat" :change="console.log('Hello World!')" name="pet" value="cat">
<input type="radio" id="dog" onchange="onPetChange('pet', 'dog')"  name="pet" value="dog">

If you provide us also your onPetChange function I can have a look on it to check if there is something wrong with it too.

发布评论

评论列表(0)

  1. 暂无评论