存储递增复选框(jQuery)

时间:2017-08-09 13:31:11

标签: javascript jquery html

我有一个自定义复选框,点击后会递增。一切正常,但我无法弄清楚如何存储增量值。这就是我到目前为止所做的:

public class Dog : RealmObject {
    public string name {get;set;}
    public Human Owner {get;set}
}

public class Human : RealmObject {
    public IList<Dog> dogs {get;}
    public string FirstName {get;set;}
}

Realm realm = Realm.GetInstance();
Human man = new Human();
man.Firstname = "Matan";

Dog dog = new Dog();
dog.name = "Jimmy";
man.dogs.Add(dog);

using (var trans = realm.BeginWrite()){
    realm.Add(man):
    trans.Commit();
}

var people = realm.All<Human>();

foreach(var person : people){
    Debug.WriteLine(person.FirstName);
    Debug.WriteLine(person.dogs[0].name);
}

// [Output]
// Matan
// 
var clicks = 0;
$('#checkbox1').click(function() {
  clicks++;
  $('.clickspan').html(clicks);
});
parseInt(document.getElementById('checkbox1').value = clicks)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='checkbox' name='array[]' id='checkbox1' value=''> <div class='count'><span class='clickspan'></span></div>给了我0.我不知何故应该能够将增量的总数存储在一个新变量中并使用它来发送回值字段,如下所示:

print_r($array)

但是如何存储“totalclicks”?

谢谢!

1 个答案:

答案 0 :(得分:0)

您只需在 加载上设置复选框的值。你需要在里面点击处理程序。您也可以删除parseInt(),因为除了位置错误之外,它还是多余的

&#13;
&#13;
$('#checkbox1').click(function() {
  $(this).val(function(i, v) {
    $('.clickspan').html(++v);
    return v;
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="array[]" id="checkbox1" value="0">
<div class="count"><span class="clickspan"></span></div>
&#13;
&#13;
&#13;