0

model私は好きです、

  def question_hash_string
    @question_hash_string || '{}'
  end

  def question_hash
    ActiveSupport::JSON.decode question_hash_string
  end

Javascript

var question_hash_element = document.getElementById('enr_rds_batch_update_question_hash_string');
var question_hash = JSON.parse(question_hash_element.value);

question_hash[batch_question_id] = (batch_answer_id || batch_rdsap_answer || batch_answer_checkbox);
question_hash_element.value = JSON.stringify(question_hash);

それは次のような値を与えるでしょう"{"16":"3","28":false}"

次のような答えで別の価値を追加したいのですが、

question_hash[batch_question_id] = ((batch_answer_id || batch_rdsap_answer || batch_answer_checkbox) && (build_id));

"{"16":"3","28":false:"2", "3":"55":"54"}"等々。':'を使用して既存のレコードに別の列を追加する必要があります。

4

2 に答える 2

2

キーと値のペアをjavascriptオブジェクトに追加する:

var question_hash = { question_id:2, answer_id:3 };
question_hash.build_id = 6;
于 2013-01-15T12:34:18.507 に答える
0

JabvaScriptコードに新しい要素を追加できます。

function doSomething(value) {
   var jsonValues = getParameters();

   jsonValues.value3 = value; //Here you add a new value in your json
}

//Example of function to generate json
function getParameters() {

    return {
        value: 'a',
        value1: 'b',
        value2: 'c'
    };
}
于 2013-01-15T12:34:58.457 に答える