如何使用 jquery/javascript 获取特定选项的复选框值?

发布于 2025-01-05 12:44:46 字数 1394 浏览 0 评论 0原文

在我的手机间隙 Android 应用程序中,我已在 div 中动态附加了复选框中的问题和相应选项。有 5 到 6 个问题,每个问题有 3 到 4 个复选框。但我的问题是我需要获取复选框特定问题的值,而不是 div 中完整复选框的值。请指导我。提前致谢。

function list(results) {
    for (i = 0; i < results.rows.length; i++) {
        $("#div").append("<li>" + results.rows.item(i).ques + "<br/>" + "</li>");
        var optiontypearray = new Array();
        var arr = results.rows.item(i).option;
        var optiontypearray = arr.split(" ");

        for (var j = 0; j < optiontypearray.length; j++) {
            $("#div").append("<input  id='chk'  name='ckbox' value='" + optiontypearray[j] + "'  type='checkbox'/>" + optiontypearray[j] + "<br/>");
        }

    }
    $("#button").append("<input type='button' value='" + submitbutton + "' onclick='submitanswer()'/>");
} /** while clicking submit button*/

function submitanswer() {

    $('li').each(function(index) {

        var text = $(this).text();
        alert(text);
        alert(index + ': ' + $(this).text());
        if (index == 0) {
            alert("s");
            $(":checkbox:checked").each(function(index, text) {
                alert(text);
                alert(index + ': ' + $(this).text());
                var a = $(this).val();
                alert(a);

            });
        }
    });
}
/**html*/
 <div id="div"></div>
 <div id="button">

In my phone gap android application, i have appended the questions and the respective options which is in check box dynamically in div.There are 5 to 6 questions with 3 to 4 check boxes each.But my problem is i need to get the check box value for particular question and not for full check boxes in the div.Please kindly guide me.Thanks in Advance.

function list(results) {
    for (i = 0; i < results.rows.length; i++) {
        $("#div").append("<li>" + results.rows.item(i).ques + "<br/>" + "</li>");
        var optiontypearray = new Array();
        var arr = results.rows.item(i).option;
        var optiontypearray = arr.split(" ");

        for (var j = 0; j < optiontypearray.length; j++) {
            $("#div").append("<input  id='chk'  name='ckbox' value='" + optiontypearray[j] + "'  type='checkbox'/>" + optiontypearray[j] + "<br/>");
        }

    }
    $("#button").append("<input type='button' value='" + submitbutton + "' onclick='submitanswer()'/>");
} /** while clicking submit button*/

function submitanswer() {

    $('li').each(function(index) {

        var text = $(this).text();
        alert(text);
        alert(index + ': ' + $(this).text());
        if (index == 0) {
            alert("s");
            $(":checkbox:checked").each(function(index, text) {
                alert(text);
                alert(index + ': ' + $(this).text());
                var a = $(this).val();
                alert(a);

            });
        }
    });
}
/**html*/
 <div id="div"></div>
 <div id="button">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天冷不及心凉 2025-01-12 12:44:46
While I'm not sure about exactly what you want to accomplish, this bit of code worked for me.

<select id="mySelect" multiple="multiple">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
  <option value="4">Fourth</option>
</select>

<script type="text/javascript"> 
$(document).ready(function() {
  if (!$("#mySelect option:selected").length)
    $("#mySelect option[value='3']").attr('selected', 'selected');
});
</script>
While I'm not sure about exactly what you want to accomplish, this bit of code worked for me.

<select id="mySelect" multiple="multiple">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
  <option value="4">Fourth</option>
</select>

<script type="text/javascript"> 
$(document).ready(function() {
  if (!$("#mySelect option:selected").length)
    $("#mySelect option[value='3']").attr('selected', 'selected');
});
</script>
沉睡月亮 2025-01-12 12:44:46

在您的 Submitanswer 函数中,您有一个 jquery every 语句。您正在寻找的元素应该是

$(':checked').each...

第二个参数实际上是元素而不是“文本”。例如,

$(":checked").each(function(index, el)...

要获取元素使用的“文本”,

$(el).html()

尽管您可以使用 $(this).html 我发现使用 $(el) 更容易阅读并防止错误,

如果您想将问题与我建议附加的响应相匹配每个问题都有一个标签。然后在每个语句中,您可以使用索引将标签与其相应的答案相匹配,例如

<div class="questions">
 <label for="question-1">Do you like ice-cream?</label>
 <input type="checkbox" name="question-1" />

 <label for="question-2">Do you want more?</label>
 <input type="checkbox" name="question-2" />
</div>

现在您可以通过这种方式找到问题和答案。

var $questions = $('label'), $answers = $(':checked');

$answers.each(function(index, el) {
 alert($questions.eq(index).html() + ' : ' + $(el).val());
});

in your submitanswer function you have a jquery each statement. the elements you are looking for should be

$(':checked').each...

The second argument is actually the element not 'text'. e.g.

$(":checked").each(function(index, el)...

To get the 'text' for the element use

$(el).html()

although you can use $(this).html i find it easier to read and prevent mistakes to use $(el)

if you want to match a question against a response i would recommend attaching a label for each question. Then in your each statement you can use the index to match a label with its corresponding answer e.g.

<div class="questions">
 <label for="question-1">Do you like ice-cream?</label>
 <input type="checkbox" name="question-1" />

 <label for="question-2">Do you want more?</label>
 <input type="checkbox" name="question-2" />
</div>

Now you can find the questions and answers in this way..

var $questions = $('label'), $answers = $(':checked');

$answers.each(function(index, el) {
 alert($questions.eq(index).html() + ' : ' + $(el).val());
});
忘东忘西忘不掉你 2025-01-12 12:44:46

您可以创建一个像这样的函数来查看是否选中了具有某个值的复选框。我不知道它是否是最佳的,但它应该可以工作::)

function isChecked(chkValue) {
        return $("input[type='checkbox']", '#div').filter(function (index) {
            return ($(this).attr('value') == chkValue);
        }).is(':checked');
    }

You can create a function like this one to see if a checkbox having some value is checked or not. I do not know if its optimal but it should work: :)

function isChecked(chkValue) {
        return $("input[type='checkbox']", '#div').filter(function (index) {
            return ($(this).attr('value') == chkValue);
        }).is(':checked');
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文