如何使用jquery或javascript存储ASP.NET hiddenfield的多个值 [英] How to store the multiple values of ASP.NET hiddenfield using jquery or javascript

查看:124
本文介绍了如何使用jquery或javascript存储ASP.NET hiddenfield的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个asp.net项目中工作,我需要一个asp.net隐藏字段值来使用jquery或javascript存储其中的数组值。因为我已经获得了值并使用函数存储在本地javascript变量中。我需要将其存储在隐藏字段中以访问服务器端的值。实际上我需要的是单选按钮检查值。



我尝试过:



I am working in a asp.net project for which i require a asp.net hidden field value to store array of values in it using jquery or javascript. Because i have got the values and stored in local javascript variable using function. That values i required to store it in hidden field to access that values in server side. Actually what i needed is radio button checked values.

What I have tried:

for (let i = 1; i <= count; i++) {
    let values = getRadioVal(document.getElementById("form1"), 'name' + i);

// With this function i am getting values that radio button checked
function getRadioVal(form, name) {
    let val;
    // get list of radio buttons with specified name
    let radios = form.elements[name];
    // loop through list of radio buttons
    for (var i = 0, len = radios.length; i < len; i++) {
        if (radios[i].checked) { // radio checked?
            val = radios[i].value; // if so, hold its value in val
            break; // and break out of for loop
        }
    }
    return val; // return value of checked radio or undefined if none checked
}





要将值存储在隐藏字段中,我尝试过这导致[Object Object]值



To store the values in hidden field i have tried this but resulting [Object Object] value

let array = [];
for (array in values)
    array.push[values];      
console.log($('#hdnValue').val(JSON.stringify(array)));





我希望将值存储在隐藏字段中如何实现此目的请任何人帮助我



I want values to be stored in hidden field how can i achieve this please anybody help me

推荐答案

('#hdnValue')。val(JSON.stringify(array) ));
('#hdnValue').val(JSON.stringify(array)));





我希望将值存储在隐藏字段中如何才能实现此目标请任何人帮助我



I want values to be stored in hidden field how can i achieve this please anybody help me


values 存储您检查的最后一个单选按钮组中的选定值。然后,您将迭代 的属性和方法 字符串类型 [ ^ ],这不是你想要的。



你不应该需要单独的字段 - 当您提交表单时,所选单选按钮的值将回发到服务器。但如果您仍想这样做,则需要将所选值推送到数组中:

values stores the selected value from the last radio button group you check. You're then iterating over the properties and methods of the string type[^], which isn't what you want.

You shouldn't need a separate field - the value of the selected radio buttons will be posted back to the server when you submit your form. But if you still want to do it, you'll need to push the selected values into an array:
let array = [];
let theForm = document.getElementById("form1");

for (let i = 1; i <= count; i++) {
    array.push(getRadioVal(theForm, 'name' + i));
}

document.getElementById('hdnValue').value = JSON.stringify(array);


这篇关于如何使用jquery或javascript存储ASP.NET hiddenfield的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆