duanjuelu8874
2011-08-29 09:37
浏览 39
已采纳

在Javascript中录制按钮ID

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) ?
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongya5893 2011-08-29 09:56
    已采纳

    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.

    点赞 打赏 评论
  • douli0531 2011-08-29 09:49

    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.

    点赞 打赏 评论

相关推荐 更多相似问题