隐藏字段值包含[对象对象]

时间:2014-04-21 16:22:05

标签: javascript jquery asp.net

我在jquery脚本中设置隐藏字段值,但隐藏字段返回包含[Object Object]

的值

以下是值;

“[object Object],2014年4月22日”

脚本

          var indx = 0;
          var hdfield = $('#hdlstVisitDates');
          var lst = $('#lstVisitDates');
          var options = $('#lstVisitDates option');
          $(options).each(function () {

              if (indx = 0) {
                  hdfield = $(this).val();
                  indx = 1;
              }
              else
              { hdfield = hdfield + ',' + $(this).val(); }
          });

          $('#hdlstVisitDates').val(hdfield);

2 个答案:

答案 0 :(得分:2)

// initialise hdfield with val()
var hdfield = $('#hdlstVisitDates').val();

否则hdfield将成为输入元素,字符串连接将其转换为[object Object]

答案 1 :(得分:1)

使用hdfield初始化$('#hdlstVisitDates')似乎毫无意义,因为您稍后会覆盖该值,同时您if (indx = 0) {将始终评估为false,它应该是if (indx == 0) {

      var indx = 0;
      var hdfield;// = $('#hdlstVisitDates'); why do this?
      var lst = $('#lstVisitDates');
      var options = $('#lstVisitDates option');
      $(options).each(function () {

          if (indx == 0) {
              hdfield = $(this).val();
              indx = 1;
          }
          else
          { hdfield = hdfield + ',' + $(this).val(); }
      });

      $('#hdlstVisitDates').val(hdfield);