开发者

Why is my checkbox input always the same?

开发者 https://www.devze.com 2023-03-14 04:47 出处:网络
I have a form (posted below) and the value submitted for the checkbox does not change.I added the value attribute to the input to try and alleviate this issue but now instead of the debugger always re

I have a form (posted below) and the value submitted for the checkbox does not change. I added the value attribute to the input to try and alleviate this issue but now instead of the debugger always reporting "active" it reports whatever value is set to say "value1" no matter if the box is checked or not.

<form enctype="multipart/form-data" id="f0" method="post" action="">
  <div class="t1">
    <div class="t1_r1">
      <div class="t1_c1"><p class="c"></p></div>
      <div class="t1_c1"><p class="c">Email开发者_运维百科</p></div>
      <div class="t1_c1"><p class="c">Password</p></div>
      <div class="t1_c1"><p class="c">Keep me logged in<input class="cb" type="checkbox" name="f0a" value="value1" /></p></div>
    </div>
    <div class="t1_r1">
      <div class="t1_c1"><input class="te6" type="text" name="f0b" /></div>
      <div class="t1_c1"><input class="te6" type="password" name="f0c" /></div>
      <div class="t1_c2"><a id="f0d" href ="javascript:void(0)" class='but'>Login</a></div>
    </div>
    <div id=fb1 class="t1_r1">
    </div>
  </div>
</form>

I put the elements in a string of text using this function which works fine for the other two inputs. I updated it to reflect the answer. This has not been tested yet.

function is(a)
{
  var b = '';
  var c = document.forms[a].elements;
  for (i = 0; i < c.length; i++)
  {
    if (c[i].type == 'checkbox' && c[i].checked == false)
    {
      b += c[i].name + "=NULL&";
    }
    else
    { 
      b += c[i].name + "=" + c[i].value + "&";
    }
  }
  b = b.slice(0, -1);
  return b;
}


That's because with checkboxes, .value always returns the text that's in the value attribute that you've set in the HTML. What you need to do is check the checked property to see if it's true. Something like:

function is(a) {
   var b = '';
   var c = document.forms[a].elements;
   for (i = 0; i < c.length; i++) {
      var value = "";
      if (c[i].type == "checkbox") {
         value = (c[i].checked) ? c[i].value : "";
      } else {
         value = c[i].value;
      }
      b += c[i].name + "=" + value + "&";
   }
   b = b.slice(0, -1);
   return b;
}


Checkboxes, despite their simple appearance, are more complicated than text - Sending Checkbox Values Link.

0

精彩评论

暂无评论...
验证码 换一张
取 消