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

How do I use Javascript to change value of a hidden input depending on status of a checkbox? - Stack Overflow

programmeradmin6浏览0评论

I am trying to change the value of a hidden input field, depending on the value of a checkbox. I don't know much about Javascript but this is what I have so far.

<input type="hidden" value="" id="delterms" name="O_" />
<input type="checkbox" id="checkbox" onchange="terms()" />
<script type="text/javascript">
 var checked = document.getElementById('checkbox').checked;
  function terms() {
   if (checked==false)
    {
     document.getElementById('delterms').value=''
    }
   else
    {
    document.getElementById('delterms').value='Accepted'
    }
}
</script>

I got it to work but only on the first click, is there anyway to set the value depending on the checkbox status? I suspect there is some far simpler way and I am no doubt over plicating the issue.

I am trying to change the value of a hidden input field, depending on the value of a checkbox. I don't know much about Javascript but this is what I have so far.

<input type="hidden" value="" id="delterms" name="O_" />
<input type="checkbox" id="checkbox" onchange="terms()" />
<script type="text/javascript">
 var checked = document.getElementById('checkbox').checked;
  function terms() {
   if (checked==false)
    {
     document.getElementById('delterms').value=''
    }
   else
    {
    document.getElementById('delterms').value='Accepted'
    }
}
</script>

I got it to work but only on the first click, is there anyway to set the value depending on the checkbox status? I suspect there is some far simpler way and I am no doubt over plicating the issue.

Share Improve this question edited Jan 28, 2012 at 17:30 PeeHaa 72.7k60 gold badges194 silver badges264 bronze badges asked Jun 7, 2011 at 15:35 DaveDave 511 gold badge1 silver badge2 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 5

Try adding an event listener instead of using the HTML "onchange" attribute, this might clarify things for you and will probably make your code easier to maintain in the long run:

<input type="hidden" id="delterms" value="" name="O_" />
<input type="checkbox" id="checkbox" />
<script type="text/javascript">
  var checkbox = document.getElementById('checkbox')
    , hidden = document.getElementById('delterms');
  checkbox.addEventListener('change', function() {
    hidden.value = (this.checked) ? 'Accepted' : '';
  }, false);
</script>

The idea is that the anonymous function gets run every time the user clicks the checkbox, which sets the value of the hidden field to either "Accepted" or the empty string, depending on whether or not the box is checked.

This jsFiddle shows a working example.

Move the variable assignment into the function, thus:

function terms() {
    var checked = document.getElementById('checkbox').checked;
    if (checked==false)

Fixed in a fiddle - use the Event of clicking.

http://jsfiddle/hSMbf/1/

Because checkboxes are not submitted in a form.submit() when they are unchecked I use a hidden field that gets updated when the checkbox is clicked on.

<input type="hidden" id="chbx" value="" name="0_">
<input type="checkbox" id="__chbx" 
    onclick="document.getElementById('chbx').value = 
             document.getElementById('__chbx').checked;"/>

(I have omitted the jsp-code that adds the checked property to the checkbox-input if it was already checked when rendering the page.)

Your variable checked is set only once, so it always equals to the inital value. Move it inside your function terms().

get the checked value inside your function

    <script type="text/javascript">

      function terms() {
       var checked = document.getElementById('checkbox').checked;
       if (checked==false)
        {
         document.getElementById('delterms').value=''
        }
       else
        {
        document.getElementById('delterms').value='Accepted'
        }
    }
    </script>
发布评论

评论列表(0)

  1. 暂无评论