vendredi 25 août 2017

Trouble with changing checkbox states on jQuery

I'm developing a game to test some properties of control codes. The based-game is this well-known magic one: you have some cards with numbers and select all cards which a number of your choice appears. Then, the "magician" can discover your number using binary code of cards you have selected. A very simple version is as follows:

card 1 (represents bit xx1 if selected)

  • 1
  • 3
  • 5
  • 7

card 2 (bit x1x)

  • 2
  • 3
  • 6
  • 7

card 3 (bit 1xx)

  • 4
  • 5
  • 6
  • 7

In example, if you are thinking on number 3, you'll select cards 1 and 2, that represents (011) in binary, that's number 3.

Well, this explanation is to put you in context. My version of the game have additionally a layer of verification cards and numbers. You can find the actual code in this codepen. It's full of alerts to give me info about variables. If you run it you'll find first execution is ok, but next tries it starts to being crazy.

I have some troubles with check/uncheck checkboxes with jQuery. I've tried many solutions and read documentation and other related posts, but nothing appears to solve this issue. I think there's some troubles with the scope of the $lie var. When I click button, it's supossed to $lie = ''; and after another function $lie gets a new value. This is correct, but when I pass $lie to this function it remembers previous value.

I'm copying here the complete skeleton of the functions that works with or are related to $lie var:

var $lie = '';
function check() {
    $lie = ''; // only to clear on successive calls
    if (something) {
        // $lie gets its correct value here
    }
    return $lie; //I have made some alerts here and it is working well always
}

function lying() {
    if ($lie === something) {
         // something related with "something"
    }
}

function message($lie) {
    // other stuff
    $(some-div).on('click', function() {
        // more stuff
        $change = $('#' + $lie); // here $lie gets the first value, but not reset on successive tries, that is why program goes crazy
        $change.prop('checked', function(i, i2) {
            return !i2;
        });
    });
}

$(run-button).on('click', function(e) {
    e.preventDefault();
    check();
    if ($lie !== '') { // do only if $lie have value
        lying();
        message($lie)
    }
}

First time I execute the program $lie have a correct value and works properly. But next runs the $lie value is not reset and remember the previous value. I'm not sure where is the mistake, I've tried to reset $lie = ''; in many places, but ever is handling the first value to the message($lie); function.

Thanks for your help and your suggestions and excuse me if the answer is in other post and I've not seen it!




How to send multiple checkbox values in single object through ajax

I have a form with 4 different checkboxes with ids namely filter_AFFILIATION_1, filter_AFFILIATION_2 and so on till 4. I am trying to send the checked checkboxes values to the server dynamically by using an ajax call. Here is my code:

    $('input[type="checkbox"]').change(function(){   
    var ids = ['filter_AFFILIATION_1','filter_AFFILIATION_2','filter_AFFILIATION_3','filter_AFFILIATION_4'];    
    for(var i = 0; i < ids.length; i++){
      if(document.getElementById(ids[i]).checked === true){
      var data = {};
      data['request'+i] =  $('#'+ ids[i]).val();            
      console.log(data);
        }
    }

     $.ajax({
            type: 'POST', 
            url: Routing.generate('listingpage'),
            contentType: 'application/x-www-form-urlencoded',
            data: data,        
        success: function(result,status,xhr){
            console.log(result);
        }

If you look at

data['request'+i] =  $('#'+ ids[i]).val();            
      console.log(data);

part of the code, the output of console.log if I click

  1. Only first checkbox:

    {request0: "1"}

  2. First and then second checkbox:

    {request0: "1"}

    {request1: "2"}

  3. First and then second and then 3rd checkbox:

    {request0: "1"}

    {request1: "2"}

    {request2: "3"}

  4. First then second, then third and then uncheck second checkbox:

    {request0: "1"}

    {request2: "3"}

Now my problem is that I want to send the data as a single object rather than multiple objects such as if the user clicks first checkbox and then second the output of console.log(data) should be

 `{request0: "1", request1: "2"}`

I've tried a lot of different methods but nothing seem to work. Any ideas?




How to define Array of checkboxes in VBA

I know similar questions have been asked before like this and this but I was having issues with initializing the checkbox array object (My VBA is quite rusty).

I have the following code:

Dim chkAdQ(4) As Checkbox

Set chkAdQ(0) = chkAdQ1
Set chkAdQ(1) = chkAdQ2
Set chkAdQ(2) = chkAdQ3
Set chkAdQ(3) = chkAdQ4

where chkAdQ1, chkAdQ2 etc. are ActiveX checkboxes present on the form. On debugging I can see that chkAdQ(4) prompts 'nothing' on the declaration itself and hence the assignment gives a Type mismatch exception.

I also tried by declaring chkAdQ(4) as an Object but to no avail. Any thoughts?




jquery check-uncheck checkbox [duplicate]

This question already has an answer here:

When I click on Problem, both check boxes must select/deselect. I want to select and deselect check boxes for multiple time. It works only for selecting and then deselecting (2 times) and then is not working any more.

check boxes

My code:

 $(document).ready(function() {
        $('#CHECK-ALL').click(function() {
            if ($(this).is(':checked')) {
                $('#P').attr('checked',true);                
            } else {
                $('#P').attr('checked',false);                
            }
        });
    });




Angular filter by checkbox, with checkbox

I want a filter for my program but i dont know how to do that.. google only gives me those searchfield filters or checkbox by names .. i dont get it.. so far..

    var app = angular.module('surveyApp', []);

app.controller('BackendCtrl', function($scope, $http){

        $scope.informations = [
            {
                id: 123,
                text: "first object",
                checkbox: 1,
            },
            {
                id: 222,
                text: "second object",
                checkbox: 1,
            },
            {
                id: 303,
                text: "third object",
                checkbox: 1,
            },
            {
                id: 412,
                text: "fourth object",
                checkbox: 0,
            },
            {
                id: 500,
                text: "fifth object",
                checkbox: 0,
            },
            {
                id: 666,
                text: "sixth object",
                checkbox: 1,
            },

        ];
   });

this is my angular data !

      <html>
<head>
    <title>Survey</title>

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
</head>
<body ng-app="surveyApp" ng-controller="BackendCtrl">  

<div style="width: 150px; height: 40px; border:1px solid;">

    <input type="checkbox"/> All objects

</div>

<div>
    <table  border="1" style="margin: 0 auto;" ng-controller="BackendCtrl">
        <tr ng-repeat="item in informations">
            <td></td>
            <td></td>
            <td><input type="checkbox" ng-model="item.checkbox" ng-true-value="1" ng-false-value="0"/>cool</td>
        </tr>
    </table>
</div>

<script src="http://ift.tt/1GzOOSL"></script>
<script src="app.js"></script> 

</body>
</html>

the first checkbox should be my filter for item.checkbox.. if its true, all numbers should be given and if its false only the "not cool" objects.. greez pls help me




jeudi 24 août 2017

Get checked checkbox value from view to controller using jQuery

i had the table like this

 <div class="container" id="changeDate">
    <table class="container table table-striped">
        <thead>
            <tr class="label-primary" style="color:white;">
                <th class="col-sm-1"><input id="CAId" name="CAId" type="checkbox" onclick="SetAllCheckBoxes(this)" /></th>
                <th class="col-sm-6">Company Name</th>
                <th class="col-sm-2">DCR Date</th>
                <th class="col-sm-2">AGM Date</th>
                <th class="col-sm-1">Action</th>
            </tr>
        </thead>
@if (comp != null)
        {
            <tbody>
                @foreach (var item in comp)
                {
                    <tr>
                        <td class="col-sm-1"><input id="CId" name="CId" type="checkbox" value="@item.Id" /></td>
                        <td class="col-sm-6">@item.Name</td>
                        <td class="col-sm-2">@item.DCRDate?.ToString("dd MMM yyyy")</td>
                        <td class="col-sm-2">@item.AGMDate?.ToString("dd MMM yyyy")</td>
                        <td class="col-sm-1">
                            <a asp-action="Edit" asp-route-Id="@item.Id"><span class="glyphicon glyphicon-edit"></span></a>
                            <a asp-action="Delete" asp-route-Id="@item.Id" onclick="return confirm('Are you sure to delete @item.Name ?');"><span class="glyphicon glyphicon-remove"></span></a>
                        </td>
                    </tr>
                }
            </tbody>
        }
else
        {
            //TOFIX - DISPLAY when no company found
            <tbody>
                <tr>
                    <td colspan="5">No Record Found!</td>
                </tr>
            </tbody>
        }
    </table>
<div class="container">
        <div class="row form-group pull-right">
            <div class="input-group col-sm-12">
                <span class="input-group-btn" style="width:120px;">
                    @Html.DropDownList("dateList", ViewData["dateList"] as IEnumerable<SelectListItem>, new { @id = "dateList", @class = "form-control" })
                </span>
                <input type="text" class="form-control" readonly="readonly" id="date" name="date" value="" style="width:150px" placeholder="@System.DateTime.Now.ToString("dd-MMM-yyyy")" />
                <button id="dateSubmitBtn" type="submit" class="btn btn-default">Change</button>
            </div>
        </div>
    </div>
</div>
}

i want to know which checkbox was selected by using jQuery. here was the code i tried, but non of it works for me.

if ($('input[name="CId"]').is(':checked')) {}

Or

if ($("#CId").is(':checked')) {}

Or

 var $boxes = $('input[name=CId]:checked');

Or

 if ($('input[name="CId"]:checked')){}

how can i get which checkbox was selected and store the value of that checkbox into a list then pass it from view to controller?




TypeError: Cannot set property 'checked' of null for CheckBox

document.getElementById("cbox0").checked = true;

I am using this statement to auto check a checkbox but i get TypeError: Cannot set property 'checked' of null for CheckBox this error. I have tried to use if statement to check if its not a null but it also give me the same error.

jsp+='<input id="cbox'+index+'" type="checkbox" name="compare" value="'+rightCard.oid+'" onchange="myFunction('+index+')"><label for="cboxCard'+index+'">&nbsp;</label>';