te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Vue.js @change and $event.target.value issue - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js @change and $event.target.value issue - Stack Overflow

programmeradmin4浏览0评论

I have this very basic HTML:

<div id="app">
  <input type="number" @change="start=$event.target.value">
  <input type="number" @change="final=$event.target.value">
  <button v-bind:disabled="isButtonActive">Proceed</button>
  {{start}} -- {{final}} -- {{res}}
</div>

And small vue.js code:

new Vue({
  el: '#app',
  data: {
      start: 0,
      final: 0
  },
  puted: {
      res() {
        return this.start < this.final;
      }
  },
  methods: {
    isButtonActive() {
      return this.start < this.final;
    }
  }
})

Now for the issue: If I put 2 to first input and 12 to second I got '2 -- 12' -- false, but why - 2 < 12?

If I put 12 to second input first and then 2 to the first I got

'2 -- 12 -- true'

If I change 2 to 45 I got '45 -- 12 -- true', oh

And now matter 'true' or 'false' button never bees active. Please help. Here is the codepen link

I have this very basic HTML:

<div id="app">
  <input type="number" @change="start=$event.target.value">
  <input type="number" @change="final=$event.target.value">
  <button v-bind:disabled="isButtonActive">Proceed</button>
  {{start}} -- {{final}} -- {{res}}
</div>

And small vue.js code:

new Vue({
  el: '#app',
  data: {
      start: 0,
      final: 0
  },
  puted: {
      res() {
        return this.start < this.final;
      }
  },
  methods: {
    isButtonActive() {
      return this.start < this.final;
    }
  }
})

Now for the issue: If I put 2 to first input and 12 to second I got '2 -- 12' -- false, but why - 2 < 12?

If I put 12 to second input first and then 2 to the first I got

'2 -- 12 -- true'

If I change 2 to 45 I got '45 -- 12 -- true', oh

And now matter 'true' or 'false' button never bees active. Please help. Here is the codepen link

Share Improve this question asked May 18, 2018 at 18:06 ParadoxetionParadoxetion 7262 gold badges11 silver badges31 bronze badges 2
  • You are making your code harder than it should be. Use v-model="start" instead of @change="start=$event.target.value" – i-- Commented May 18, 2018 at 18:20
  • If you want to use the method, you have to actually call the method, v-bind:disabled="isButtonActive()". codepen.io/Kradek/pen/GdPrVY?editors=1010 – Bert Commented May 18, 2018 at 18:24
Add a ment  | 

2 Answers 2

Reset to default 7

because the value of $event.target.value is one string, not number, you can convert string to number by parseInt. check below demo.

Vue.config.productionTip = false
new Vue({
  el: '#app',
  data: {
      start: 0,
      final: 0
  },
  puted: {
      res() {
        console.log(typeof this.start, typeof this.final, this.start < this.final)
        console.log('[Use ParseInt]', parseInt(this.start) < parseInt(this.final))
        return this.start < this.final;
      }
  },
  methods: {
    isButtonActive() {
      return this.start < this.final;
    }
  }
})
<script src="https://unpkg./[email protected]/dist/vue.js"></script>
<div id="app">
  <input type="number" @change="start=$event.target.value">
  <input type="number" @change="final=$event.target.value">
  <button v-bind:disabled="isButtonActive">Proceed</button>
  {{start}} -- {{final}} -- {{res}}
</div>

You can use the modifier= v-model.number then you should v-bind:disabled with the result(isButtonActive()) of the function instead of the function itself(isButtonActive), or use v-bind:disabled="res" will be better. (the difference between puted properties and methods is that puted properties are cached based on their dependencies), you can hit click me button to see the differences.

like below demo:

Vue.config.productionTip = false
new Vue({
  el: '#app',
  data: {
      start: 0,
      final: 0,
      testText: 'You will see isButtonActive is invoked, but puted properties will not'
  },
  puted: {
      res() {
        console.log('puted=res', typeof this.start, typeof this.final)
        return this.start < this.final;
      }
  },
  methods: {
    isButtonActive() {
      console.log('isButtonActive', typeof this.start, typeof this.final)
      return this.start < this.final;
    },
    test: function () {
      this.testText += '!'
    }
  }
})
<script src="https://unpkg./[email protected]/dist/vue.js"></script>
<div id="app">
  <input type="number" v-model.number="start">
  <input type="number" v-model.number="final">
  <button v-bind:disabled="isButtonActive()">Proceed</button>
  <button v-bind:disabled="res">Proceed</button>
  {{start}} -- {{final}} -- {{res}}
  <hr>
  <button @click="test()">Click me!!!</button>
  <p>{{testText}}</p>
</div>

There is no need to convert string to number. You can simply get the value as a number without transforming it :

$event.target.valueAsNumber

More detail: https://stackoverflow./a/68177209/12247829

发布评论

评论列表(0)

  1. 暂无评论