简体   繁体   English

通过jQuery和checboxes管理表单的输入

[英]Managing form's inputs via jQuery and checboxes

I have a group of checkboxes which represents rows from a code snippet. 我有一组复选框,它们代表代码片段中的行。 I'd love to select a portion of that code with just two clicks (on the first and last row of the code) of checkboxes, check the code in the middle and set two input fields with starting and ending line of the selection. 我很想通过单击复选框(在代码的第一行和最后一行)两次单击来选择该代码的一部分,检查中间的代码,并用选择的开始和结束行设置两个输入字段。 Here's my code but it doesn't seem to work at all. 这是我的代码,但似乎根本不起作用。

HTML CODE HTML代码

<div id="example">    
     <input type="checkbox" name="box1" id="box1">
     <label for="box1"> code line 1 </label>
     <input type="checkbox" name="box2" id="box2">
     <label for="box2"> code line 2 </label>
     <input type="checkbox" name="box3" id="box3">
     <label for="box3"> code line 3 </label>
     <input type="checkbox" name="box4" id="box4">
     <label for="box4"> code line 4 </label>
     <input type="checkbox" name="box5" id="box5">
     <label for="box5"> code line 5 </label>  
</div>
<form name="addhint" method="post" action="">
     <input type="hidden" name="start" value="0">
     <input type="hidden" name="stop" value="0">
     [...]
</form>

JQUERY CODE JQUERY代码

$('#example input:checkbox').click(function() {
var $this = $(this);
var $id= $this.id;
if ($this.is(':checked')) {
    if ($('#form input[name=start]').val() == 0) {
        $('#form input[name=start]').val($id);
    }
    else {
         var $start= $('input[name=start]').val();
         if ($id - $start > 1) {
            for ($i = $start+1; $i < $id; $i++) {
                 $('#example input[id=box'+$i+']').attr('checked', true);
                 $('#example input[id=box'+$i+']').attr('disabled', 'disabled');
            }
            $('#form input[name=stop]').val($id);
         }
    }
} else {
       // do other stuff
}
});

and when i click a checkbox nothing happens. 当我单击一个复选框没有任何反应。 It's the first time I use jQuery so many things can be wrong. 这是我第一次使用jQuery,所以很多事情都可能出错。 Just don't kill me.. :) 只是不要杀了我.. :)

ID cannot be alone numeric. ID不能是单独的数字。

Conventions for ID: ID约定:

Must contain at least one character 必须至少包含一个字符

Must not contain any space characters 不得包含任何空格字符

Do NOT start an ID name with a number 请勿以数字开头ID名称

Try: 尝试:

$('#example input:checkbox').click(function() {
var $this = $(this);
var $id= this.id;
$id = $id.substr(3);//because you have id like box1 so remove box and get 1
if ($this.is(':checked')) {
    if ($('#form input[name=start]').val() == 0) {
        $('#form input[name=start]').val($id);
    }
    else {
         var $start= $('input[name=start]').val();
         if ($id - $start > 1) {
            for ($i = $start+1; $i < $id; $i++) {
                 $('#example input[id=box'+$i+']').attr('checked', true);
                 $('#example input[id=box'+$i+']').attr('disabled', 'disabled');
            }
            $('#form input[name=stop]').val($id);
         }
    }
} else {
       // do other stuff
}
});

Working Fiddle 工作小提琴

HTML 的HTML

added id to form as you were using id of form in js but not defined . 就像在js中使用form的id一样,但未定义,所以添加了id到form。

<form id="form" name="addhint" method="post" action="">

js js

$('#example input:checkbox').click(function () {
    var $this = $(this);
    var $id = this.id; //change here 
    if ($this.is(':checked')) {
        if ($('#form input[name=start]').val() == 0) {
            $('#form input[name=start]').val($id);
        } else {
            var $start = $('input[name=start]').val();
            if ($id - $start > 1) {
                for ($i = $start + 1; $i < $id; $i++) {
                    $('#example input[id=' + $i + ']').attr('checked', true);
                    $('#example input[id=' + $i + ']').attr('disabled', 'disabled');
                }
                $('#form input[name=stop]').val($id);
            }
        }
    } else {
        // do other stuff
    }
});

changed 变了

var $id = $this.id;

to

var $id = this.id; //or you can use var $id = $this.attr('id');

You got an error in your jQuery code : 您的jQuery代码中出现错误:

var $this = $(this);
var $id= $this.id;

if $this is a jQuery object, $this.id is undefined. 如果$this是jQuery对象,则$this.id是未定义的。 It should be 它应该是

var $id=$this.attr('id');

or 要么

var $id=this.id;

I would recommend separating "box" from numeric id with an underscore (or such) for easy separation of prefix and actual numeric id, like this: 我建议用下划线(或类似的数字)将“ box”与数字ID分开,以便于前缀和实际数字ID的容易分离,如下所示:

<div id="example">    
     <input type="checkbox" name="box_1" id="box_1">
     <label for="box_1"> code line 1 </label>
     <input type="checkbox" name="box_2" id="box_2">
     <label for="box_2"> code line 2 </label>
     <input type="checkbox" name="box_3" id="box_3">
     <label for="box_3"> code line 3 </label>
     <input type="checkbox" name="box_4" id="box_4">
     <label for="box_4"> code line 4 </label>
     <input type="checkbox" name="box_5" id="box_5">
     <label for="box_5"> code line 5 </label>  
</div>
<form name="addhint" method="post" action="">
     <input type="text" name="start" id="start" value="0">
     <input type="text" name="stop" id="stop" value="0">
</form>

You can use 您可以使用

$('#example input[type="checkbox"]').filter(':checked').size()

to check how many checkboxes are checked and take actions accordingly, something like this would make it: 要检查选中了多少个复选框并采取相应的措施,类似这样的方法将使其:

$('#example input[type="checkbox"]').change(function() {
    var count_checked = $('#example input[type="checkbox"]').filter(':checked').size()
    , start
    , stop;

    $('#example').find('[disabled="disabled"]').removeAttr('disabled');

    if (count_checked > 1) { // start and stop selected
        var id1 = $('#example input:checkbox').filter(':checked').first().attr('id').split('_')[1]
          , id2 = $('#example input:checkbox').filter(':checked').last().attr('id').split('_')[1];

        start = Math.min(id1, id2)
        stop = Math.max(id1, id2);

        for (i=start+1; i<stop; i++) { // loop through checkboxes between start and stop
            $('#box_'+i).attr('checked','checked').attr('disabled','disabled');
        }

    } else if (count_checked == 1) { // only start selected
        start = $('#example input:checkbox').filter(':checked').first().attr('id').split('_')[1];
        stop = $('#example input:checkbox').last().attr('id').split('_')[1];
    } else { // none selected
        start = 0;
        stop = 0;
    }

    $('#start').val(start);
    $('#stop').val(stop);                                            
});    

I created a jsfiddle for you to test out http://jsfiddle.net/8j9eu/2/ 我为您创建了一个jsfiddle来测试http://jsfiddle.net/8j9eu/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM