如何勾选“复选框"动态 - jQuery Mobile [英] How to check "checkbox" dynamically - jQuery Mobile

查看:31
本文介绍了如何勾选“复选框"动态 - jQuery Mobile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 HTML 创建一个复选框,如下所示:

With HTML a checkbox is created like this:

<form>
   <input type="checkbox" id="category1">Category1<br>
</form>

使用 javascript 我们可以像这样检查复选框:

With javascript we can check the checkbox like this:

$("#category1")[0].checked = true

现在我正在尝试使用 jquery-mobile 创建相同的页面.复选框如下所示:

Now I am trying to create the same page with jquery-mobile. The checkbox looks like this:

<form>
    <label>
        <input name="checkbox-0 " type="checkbox">Check me
    </label>
</form>

为什么这里没有id?名称是 id 吗?我应该删除属性名称并创建一个名为 id 的属性吗?

Why is there is no id here? Is the name the id? Should I delete the attribute name and create one with the name id?

如何使用 Javascript/jQuery 在此处选中此复选框?

How can I check this checkbox here with Javascript/jQuery?

我尝试了上面的代码,但它似乎不适用于此复选框.

I tried the code above, but it doesn't seem to work for this checkbox.

推荐答案

改变它的'.prop后需要刷新,使用.checkboxradio('refresh').这是在 jQuery Mobile 中选中复选框/单选框的正确方法.

You need to refresh it after changing its' .prop, using .checkboxradio('refresh'). This is the correct way to check checkbox/radio in jQuery Mobile.

演示

$('.selector').prop('checked', true).checkboxradio('refresh');

参考:jQuery Mobile API

这篇关于如何勾选“复选框"动态 - jQuery Mobile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆