简体   繁体   English

使用JQuery和javascript单击按钮后,如何更改嵌套对象数组中的值?

[英]How can I change the value in a nested array of objects, after a button click using JQuery and javascript?

I'm trying to make a voting app using Node.js, Javascript, JQuery, Jade and a json file for practice. 我正在尝试使用Node.js,Javascript,JQuery,Jade和json文件制作投票应用程序进行练习。

In short, I'm trying to count the votes for each option to eventually render it into a chart. 简而言之,我试图计算每个选项的票数,最终将其呈现为图表。 Specifically I was trying to add + 1 to the object value {"value": 0} in the json each time someone chooses a radio button correlated to the option and hits submit. 具体来说,每次有人选择与该选项相关的单选按钮并点击提交时,我都试图在json中的对象值{"value": 0}上添加+ 1。 I can get to the eg {"option": "Puppies","value": 0} in my JQuery and I've been trying to find a way to change the object value within the $("#btnVote").click(function(){}) . 我可以在我的JQuery中进入例如{"option": "Puppies","value": 0} ,并且我一直在尝试寻找一种方法来更改$("#btnVote").click(function(){}) object中的对象值$("#btnVote").click(function(){}) I'm not sure if that would be possible or the best way. 我不确定这是否可能或最佳方法。

Here is a sample of the JSON database. 这是JSON数据库的示例。

  [{
"name": "Puppies or Kitties",
"id": "f2754172-1c58-41ed-ae84-74e046888adb",
"choices" : [{"option": "Puppies","value": 0}, {"option": "Kittens","value": 0}],
"admin": true}]

Here is the Jade I am using to dynamically render the poll options. 这是我用来动态渲染投票选项的Jade。

form(method="post")
    fieldset.form-group
        h1 #{poll.name}
    form
        table
            each choice in poll.choices
                tr
                    td=choice.option

                        input(type='radio',value=choice.value id=choice.option name="Answer" class="clsAnswer", style='margin:10px')

        br
        input(type="button", value="Submit Vote", id="btnVote")

A visual if it helps Here is the JQuery I've used so far; 如果它有帮助的视觉效果这是我到目前为止使用的JQuery。

//Radio button vote function
$("#btnVote").click(function () {
    var selected = $(".clsAnswer:checked");
    if (!selected.val()) {
        alert("No Choice Was Made!");
    }
    else {
        var optionName = $('input[type=radio][name=Answer]:checked').attr('id');
        var selectedValue = selected.val();
        selectedValue = parseInt(selectedValue);
        alert("Value: " + selectedValue + "  " +  optionName);
    };
});

Thank you all in advance! 谢谢大家!

There are two ways that you can change the object value in this case, either through your JSON being in a database such as mongodb or through cookies as a temporary change. 在这种情况下,您可以通过两种方式更改对象值,或者通过在mongodb等数据库中的JSON或通过cookie作为临时更改。

The current JQuery is set up to receive the static html and not from the JSON file. 当前的JQuery设置为接收静态html,而不是从JSON文件接收。

Since the JSON file is used as practice in the folder hierarchy and static (as well as the html), this current setup will not render updating the object value in JSON or even as a temporary change as you would maybe see in cookies. 由于JSON文件在文件夹层次结构和静态(以及html)中被用作实践,因此当前的设置将不会呈现更新JSON中的对象值,甚至不会像在cookie中看到的那样作为临时更改。 One other alternative would be to use node.js and create a new JSON file but that is not what is being aimed at in this application. 另一种选择是使用node.js并创建一个新的JSON文件,但这不是此应用程序所针对的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击后如何使用JavaScript或jQuery更改Submit按钮的值? - How to change the value of submit button after click using JavaScript or jQuery? 如何更改 javascript 中嵌套数组对象的值 - How to Change value of nested array objects in javascript 如何使用 jQuery 在单击按钮时将 CSS 自定义属性的值更改为 1? - How can I change the value of CSS custom property by 1 on button click using jQuery? 如何使用Javascript或Jquery将JSON对象包装在数组中? - How can I wrap JSON objects in an array using Javascript or Jquery? 使用 JavaScript 打开引导模式后,如何更改单击按钮的值? - How can i change value of clicked button after open a bootstrap modal using JavaScript? Javascript 数组在按钮点击后实时变化 - Javascript array change in realtime after button click 如何在不改变容器的情况下更改数组值(对象数组内部)? javascript - How can I change array value(inside array of objects) without mutating the container? javascript 单击按钮后如何将Javascript数组传递给PHP数组? - How can I pass Javascript array to a PHP array on button click? 如何遍历javascript中的深度嵌套对象数组? - How can I iterate through array of deeply nested objects in javascript? 如何在Javascript或jQuery中更改单选按钮数组的值? - How to change value of radio button array in Javascript or jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM