【问题标题】:passing value of check box using ajax with name attribute as key and not id as key使用 ajax 以 name 属性作为键而不是 id 作为键传递复选框的值
【发布时间】:2015-10-13 20:39:21
【问题描述】:

我想知道我们如何使用 ajax 传递复选框值,例如使用 name 属性而不是 id 属性

<input type="checkbox"   id="s1" value="1" checked>S1&nbsp;
            <input type="checkbox"  id="s2" value="2" checked>S2<br>
            <input type="checkbox"  id="s3" value="3" checked>S3&nbsp;
            <input type="checkbox"  id="s4" value="4" checked>S4</td>

如果我使用 id 作为属性传递此复选框值,如下所示,

.ajax({
            type: 'POST',
            url: 'getData',
            data: {
              fromDate: $('#fromDate').val(),
              toDate: $('#toDate').val(),
              location: $('#location').val(),
              section: $('#section').val(),
              s1: $('#s1').is(":checked"),
              s2: $('#s2').is(":checked"),
              s3: $('#s3').is(":checked"),
              s4: $('#s4').is(":checked"),
        },

然后在控制器中,我使用单独的 request.getparameters("key"); 获取这些值;

但我想给下面的复选框

input type="checkbox"   name="s" value="1" checked>S1&nbsp;
        <input type="checkbox"  name="s" value="2" checked>S2<br>
        <input type="checkbox"  name="s" value="3" checked>S3&nbsp;
        <input type="checkbox"  name="s" value="4" checked>S4</td>

从此使用 ajax 我想像下面那样做

.ajax({
        type: 'POST',
        url: 'getData',
        data: {
          fromDate: $('#fromDate').val(),
          toDate: $('#toDate').val(),
          location: $('#location').val(),
          section: $('#section').val(),
          s: $('#s').val()
    },

在控制器中我想使用 request.getParameterValues("s");这将返回字符串[]。

你能帮我得到这个吗,实际上我是在普通的 Action/submit 中得到这个的,但是在 ajax 中我只得到空值..

【问题讨论】:

    标签: ajax checkbox controller


    【解决方案1】:

    尝试类似s: $('input[name="s"]:checked').val()

    【讨论】:

    • 是的,无论您建议什么,始终将尺寸设为 1,我尝试如下所示,它成功了,谢谢 --->>> var s = []; $("input:checkbox[name=s]:checked").each(function() { s.push($(this).val()); });
    猜你喜欢
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多