开发者

Recording button ID's in Javascript

I am trying to create an item selection menu, where the user is presented with multiple buttons to pick their choices. I am creating several divs with PHP.

     <div id="h1-1"><input type="button" 开发者_运维技巧value="h1-1" onclick="recordValue()"></div>
     <div id="h1-2"><input type="button" value="h1-2" onclick="recordValue()"></div>

I would like to record all the values of buttons pressed and show these values via Ajax. Then when user is done selecting and presses submit I will insert these into MSSQL database.

  • How do I record button values to an array with Javascript?
  • How do I then show what was pressed?
  • Where could I execute some filtering of the selections (e.g. if h1-1 was pressed i do not want them to be able to select h2-1) ?


I would keep the value in an array.
change you onclick part to
onclick="recordValue(this)"
and create an array outside the scope of the recordValue function wherever you declare it

var values = [];
function recordValue(element) {
    values.push(element.value);
    // code here to display value somewhere on the page
}

and then somewhere listen for the form's submit event and then send the data in your array via ajax to your server.


Your PHP-File could return a JSON-String with the values. Example: [ "Value 1", "Value 2", "Value 3" ]. This would return an array with all field-values.

You can then parse the JSON-string from your AJAX-call: var values = JSON.parse(returnVal);

For more details on JSON see json.org

Hope this is what you were looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜