HTML Checkbox onclick每次发送相同的值

时间:2017-03-20 08:30:42

标签: javascript php jquery

我尝试使用两个复选框,值为“on”。另一个是提交表格后的rember。每次我点击这个复选框,他们将始终发送“开”。

我做错了什么? 这是我的代码

var rdt = {
  updateScroll: function() {
    alert('klick scroll:' + gbi('scrollit').value);
    gbi('main').removeChild(gbi('icontent'));
    var ifr = document.createElement('iframe');
    ifr.id = 'icontent';
    ifr.src = gbi('urlinput').value;
    ifr.scrolling = (gbi('scrollit').value == 'on') ? 'yes' : 'no';
    gbi('scroll_hid').value = (gbi('scrollit').value == 'on') ? 'on' : 'off';
    gbi('main').appendChild(ifr);
    rdt.resizeView();
  }


}

function gbi(iD) {
  return document.getElementById(iD);
}
<input type="checkbox" id="scrollit" onclick="rdt.updateScroll()" checked>
<input type="hidden" name="scroll" id="scroll_hid" value="<?php echo $scroll ?>">

参数$ scroll可以预先定义URL,但如果我使用它或者我不使用它,结果是相同的。

3 个答案:

答案 0 :(得分:0)

这是因为您的value及其valueon。检查时必须获取值,如:

var value = null;
$('#scrollit').click(function(){
    if($(this).is('checked'))
    {
        value = $(this).val();
    }
});

在这种情况下,除非null

,否则您将获得其值

答案 1 :(得分:0)

复选框的主要目的是让用户选择要检查或取消选中它不打算发送值

您可以参考此link了解详情

这是主要的事情

  

HTML输入元素允许您选择   提交表单(或不提交)的单一值。

您的问题的解决方案如下,您可以通过使用以下内容检查复选框的当前状态,即是否已选中

基于您的代码

gbi('scrollit').checked

&#13;
&#13;
var rdt = {
  updateScroll: function() {
    alert('klick scroll:' + gbn('scrollit').value);
    gbi('main').removeChild(gbi('icontent'));
    var ifr = document.createElement('iframe');
    ifr.id = 'icontent';
    ifr.src = gbi('urlinput').value;
    ifr.scrolling = (gbi('scrollit').value == 'on') ? 'yes' : 'no';
    gbi('scroll_hid').value = (gbi('scrollit').value == 'on') ? 'on' : 'off';
    gbi('main').appendChild(ifr);
    rdt.resizeView();
  }
}

function gbi(iD) {
  return document.getElementById(iD);
}


function gbn(name) {
  var x = document.getElementsByName(name);
  var i;
  var element;
  for (i = 0; i < x.length; i++) {
    if (x[i].type == "checkbox") {
      element = x[i];
    }
  }
  return element;
}
&#13;
<input type="checkbox" name="scroll" id="scrollit" onclick="rdt.updateScroll()" checked>
<input type="hidden" name="scroll" id="scroll_hid" value="<?php echo $scroll ?>">
&#13;
&#13;
&#13;

答案 2 :(得分:0)

添加一个小循环使代码更清晰,并且已经可以

updateScroll: 

    function() {


                gbi('main').removeChild(gbi('icontent'));
                var ifr = document.createElement('iframe');
                ifr.id='icontent';
                ifr.src=gbi('urlinput').value;

        if (gbi('scrollit').value=='on' && gbi('scroll_hid').value!='on')
        { ifr.scrolling='yes'; gbi('scroll_hid').value='on';}
        else
        { ifr.scrolling='no'; gbi('scroll_hid').value='';}

                //ifr.scrolling=(gbi('scrollit').value=='on')?'yes':'no';
                //gbi('scroll_hid').value=(gbi('scrollit').value=='on')?'on':'off';
                gbi('main').appendChild(ifr);
                rdt.resizeView();
            },
            rotate: function() {
                rdt.w = parseInt(gbi('icontent').style.width);
                rdt.h = parseInt(gbi('icontent').style.height);
                gbi('icontent').style.width=gbi('icontentfoot').style.width = rdt.h+'px';
                gbi('icontent').style.height = rdt.w+'px';
            }