Nodeでcheckboxの値取得をこんな感じで対処しました

チェックボックで選択された項目が一つだと配列で変換されない件、こんな感じで対処しました。

悩みの詳細はこちらを参照して下さい。

※asyncでばらしています。。

var express = require('express');
var router = express.Router();
var async = require('async');

/* GET users listing. */
router.post('/', function(req, res, next) {
 console.log( "body" );
 console.dir( req.body );
 console.log( req.body.abc );

 let v001s = req.body["v001"];
 let ar001s = [];

 if( v001s == null ) {
  console.log( 'non data' );
  res.send('respond with a resource');
 } else {
  if( Object.prototype.toString.call( v001s) === '[object Array]') {
   console.log( "v001s is array" );
   console.log( "length00:" + v001s.length );
   ar001s = v001s;
 } else {
  console.log( "v001s is non array" );
  console.log( "v001s --> " + v001s );
  ar001s[0] = v001s;
 }
 async.each( ar001s, function( val, callback ) {
  console.log( val );
  callback();
 }, function( err ) {
  if( err ) {
   console.log( "err --> " + err );
  } else {
   res.send('respond with a resource');
  }
 });
}
});

module.exports = router;

1)処理用の配列を用意

2)なんか選択されているか?
→特に処理せず

3)複数選択されているか?
→複数の時は配列をコピー
→1つの時は値をpushする

4)eachで処理する

こんな感じで処理をすると

1つのときも

{ abc: ’10’, v001: ’10’ }
10
v001s is non array
v001s –> 10
10

と返ってきてるれました。

終わりに。。。

ほんとにこんなことやらなきゃいけないのかなぁ(^^;

 

 

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です