最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Undefined values for JQuery checkboxes - Stack Overflow

programmeradmin1浏览0评论

I have 3 checkboxes but they are showing as undefined in an alert box. Is there a trick to getting them to show a value? I tried putting a value of 1 in the input tag but it still reports as undefined.

Ok, thanks.. Here is some code.

        else if (item.field == "admCustRptDly" && item.value == "1")
        {
          $('#admCustRptDly').attr('checked', true);
        }

        else if (item.field == "admCustRptSumm" && item.value == "1")
        {
          $('#admCustRptSumm').attr('checked', true);
        }

        else if (item.field == "admCustRptDtl" && item.value == "1")
        {
          $('#admCustRptDtl').attr('checked', true);
        }

<input type="checkbox" id="admCustRptDly" name="admCustRptDly" class="admChkbx">
<input type="checkbox" id="admCustRptSumm" name="admCustRptSumm" class="admChkbx">
<input type="checkbox" id="admCustRptDtl" name="admCustRptDtl" class="admChkbx">

I have 3 checkboxes but they are showing as undefined in an alert box. Is there a trick to getting them to show a value? I tried putting a value of 1 in the input tag but it still reports as undefined.

Ok, thanks.. Here is some code.

        else if (item.field == "admCustRptDly" && item.value == "1")
        {
          $('#admCustRptDly').attr('checked', true);
        }

        else if (item.field == "admCustRptSumm" && item.value == "1")
        {
          $('#admCustRptSumm').attr('checked', true);
        }

        else if (item.field == "admCustRptDtl" && item.value == "1")
        {
          $('#admCustRptDtl').attr('checked', true);
        }

<input type="checkbox" id="admCustRptDly" name="admCustRptDly" class="admChkbx">
<input type="checkbox" id="admCustRptSumm" name="admCustRptSumm" class="admChkbx">
<input type="checkbox" id="admCustRptDtl" name="admCustRptDtl" class="admChkbx">
Share Improve this question edited Apr 3, 2009 at 9:06 asked Apr 3, 2009 at 8:54 TomTom
Add a ment  | 

3 Answers 3

Reset to default 2

your jquery is off, this doesn't quite give the response you'd expect, rather to find if a check box is checked:

var checked = $("#admCustRptDtl:checked").val();

Also, the checked attribute will never equal true, the html is actually checked="checked"

 else if (item.field == "admCustRptDly" && item.value == "1")

I don't understand what are you trying to do here? I'm guessing you are trying to verify the value of "admCustRptDply" checkbox. Maybe post also the code before this. You can get the value like this:

 var val = $("#admCustRptDly").val()

But in your HTML the checkboxes don't have a value attribute.

You can get the checked property of a checkbox like this:

var checked = $("#admCustRptDly").attr("checked")

And you can set it like this:

$("#admCustRptDly").attr("checked","checked")

Could you please expand? Some example code would be very helpful, you might've just misspelt the checkbox name when trying to use it.

发布评论

评论列表(0)

  1. 暂无评论