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

javascript - @click event not triggering alongside element with @blur - Stack Overflow

programmeradmin1浏览0评论

I've got a problem when working with VueJS which is kinda getting on my nerfs since I know that it probably relies on something very small but still I'm here trying to get it running for hours.

I'm using a template which should represent a searchbar with the solutions under it. To show the solutions, I'm looping over an object (I'm not done including the filtering of the solutions to that point - wanted to finish this one first). The thing I was trying to do now was filling in the searchbar value with the value I select in the solutions (passing it with a click event). But somehow my click event doesn't trigger.

I've got the following code-snippet:

<template>
    <div class="input-group">
        <div class="input-group-prepend">
            <div class="input-group-text">&#128269;</div>
        </div>
        <input 
            type="text" 
            class="form-control"
            @focus="searchFocus = true"
            @blur="searchFocus = false"
            v-model="searchQuery"
            placeholder="Search for vehicles..">
        <div class="container p-0">
            <div class="dropdown-menu search" v-show="searchFocus">
                <a class="dropdown-item bus" href="#" 
                    v-for="vehicle in vehicleObjects" 
                    :key="vehicle.id.key"
                    v-on:click="setSelectedToQuery(vehicle)">
                    <span class="ml-4">Bus {{ vehicle.id.name }}
                        <span class="badge badge-secondary"> {{ vehicle.licenseNumber }}</span>
                    <span>
                </a>
            </div> 
        </div>
    </div>
</template>

Am I missing something quite obvious? How can I get the click-event running/triggering?

Thanks in advance

Best Regards

I've got a problem when working with VueJS which is kinda getting on my nerfs since I know that it probably relies on something very small but still I'm here trying to get it running for hours.

I'm using a template which should represent a searchbar with the solutions under it. To show the solutions, I'm looping over an object (I'm not done including the filtering of the solutions to that point - wanted to finish this one first). The thing I was trying to do now was filling in the searchbar value with the value I select in the solutions (passing it with a click event). But somehow my click event doesn't trigger.

I've got the following code-snippet:

<template>
    <div class="input-group">
        <div class="input-group-prepend">
            <div class="input-group-text">&#128269;</div>
        </div>
        <input 
            type="text" 
            class="form-control"
            @focus="searchFocus = true"
            @blur="searchFocus = false"
            v-model="searchQuery"
            placeholder="Search for vehicles..">
        <div class="container p-0">
            <div class="dropdown-menu search" v-show="searchFocus">
                <a class="dropdown-item bus" href="#" 
                    v-for="vehicle in vehicleObjects" 
                    :key="vehicle.id.key"
                    v-on:click="setSelectedToQuery(vehicle)">
                    <span class="ml-4">Bus {{ vehicle.id.name }}
                        <span class="badge badge-secondary"> {{ vehicle.licenseNumber }}</span>
                    <span>
                </a>
            </div> 
        </div>
    </div>
</template>

Am I missing something quite obvious? How can I get the click-event running/triggering?

Thanks in advance

Best Regards

Share Improve this question edited Apr 29, 2020 at 19:47 Daniel 35.8k17 gold badges114 silver badges161 bronze badges asked Apr 29, 2020 at 18:37 Furkan SelcukFurkan Selcuk 552 silver badges9 bronze badges 6
  • 1 It looks fine to me. Did you breakpoint setSelectedToQuery to make sure it actually isn't entering the function? There are no errors on the console right? – user120242 Commented Apr 29, 2020 at 18:44
  • Does a click event works in inner spans in 'a' tag? – Anatoly Commented Apr 29, 2020 at 18:44
  • Also, are you using .stop to prevent bubbling or preventing default anywhere above the ponent? – user120242 Commented Apr 29, 2020 at 18:45
  • @user120242 yeah I'm trying to catch it with alerts & console.logs but yeah.. nothing and no, I'm not using .stop – Furkan Selcuk Commented Apr 29, 2020 at 18:49
  • @Anatoly nope, same result => nothing.. :/ – Furkan Selcuk Commented Apr 29, 2020 at 18:50
 |  Show 1 more ment

2 Answers 2

Reset to default 7

There are few things "off" I'll list them by level of impact

  • @blur fires before click, essentially invalidating @click
  • anchors <a ...> need to prevent redirect. You can use @click.prevent="... to prevent event propagation, or use another element, since you don't have href defined anyway.
  • one of your span elements is not closed (<span> instead of </span at the end)

To handle @blur blocking @click, you could use @mousedown instead of @click, since it executes first.

Maybe it's the solution: close your last span

Click work for me: https://codesandbox.io/s/morning-browser-fgftf

发布评论

评论列表(0)

  1. 暂无评论