質問するログイン新規登録

質問編集履歴

1

具体的なコードを追加

2017/10/04 05:51

投稿

garamond
garamond

スコア150

title CHANGED
File without changes
body CHANGED
@@ -1,11 +1,31 @@
1
1
  [yubinbango.js](https://github.com/yubinbango/yubinbango) で、住所が取得できた瞬間をイベントで検知したいんですが、うまく行きません。
2
2
  jQuery で $('input[name=prefecture]').on('change') などとしてもダメです。
3
+ ```HTML
4
+ <form class="h-adr">
5
+ <span class="p-country-name" style="display:none;">Japan</span>
6
+ <input type="text" name="zip" class="p-postal-code">
7
+ <input type="text" name="prefecture" class="p-region">
8
+ <input type="text" name="city" class="p-locality">
9
+ <input type="text" name="address" class="p-street-address p-extended-address">
10
+ <input type="text" name="city_code">
11
+ </form>
12
+ ```
3
13
  ```JavaScript
14
+ $(function(){
4
- $('input[name=zip]').on('keypress keyup', function(){
15
+ $('input[name=zip]').on('keyup', function(){
5
- // input[name=zip] には class="p-postal-code" してあります
16
+ // $('input[name=city]').on('change', function(){ が理想
6
- if ($('input[name=prefecture]').val()) {
17
+ if ($('input[name=city]').val()) {
7
- // 拾えない
18
+ // 取得した瞬間には反応しない
19
+ $.post('/get/city/code', { // 市区町村コードを取得するPHPプログラム呼び出し
20
+ prefecture: $('input[name=prefecture]').val(),
21
+ city: $('input[name=city]').val()
22
+ }, 'json').done(function(r){
23
+ $('input[name=city_code]').val(r.code);
24
+ }).fail(function(){
25
+ alert('取得失敗');
26
+ });
8
- }
27
+ }
28
+ });
9
29
  });
10
30
  ```
11
31
  という上記のコードもうまく動作しませんでした。