[英]how to check checkbox event dynamically created in javascript using jquery mobile
[英]How to check "checkbox" dynamically - jQuery Mobile
使用 HTML 創建一個復選框,如下所示:
<form>
<input type="checkbox" id="category1">Category1<br>
</form>
使用 javascript,我們可以像這樣檢查復選框:
$("#category1")[0].checked = true
現在我正在嘗試使用 jquery-mobile 創建相同的頁面。 復選框如下所示:
<form>
<label>
<input name="checkbox-0 " type="checkbox">Check me
</label>
</form>
為什么這里沒有id
? 名字是id
嗎? 我應該刪除屬性名稱並創建一個名稱為id
的屬性嗎?
如何使用 Javascript/jQuery 在此處選中此復選框?
我嘗試了上面的代碼,但它似乎不適用於此復選框。
您需要在使用.checkboxradio('refresh')
更改其.prop
之后刷新它。 這是在jQuery Mobile中選中復選框/單選按鈕的正確方法。
$('.selector').prop('checked', true).checkboxradio('refresh');
你可以做:
$('input[name="checkbox-0"]').prop("checked", true).checkboxradio('refresh'); //sets the checkbox
var isChecked = $('input[name="checkbox-0"]').prop("checked"); //gets the status
直接來自jQ Mobile文檔 :
$("input[type='checkbox']").attr("checked",true);
使用@Omar 的解決方案,我得到了錯誤:
未捕獲的錯誤:無法在初始化之前調用 checkboxradio 上的方法; 試圖調用方法“刷新”
我實際上有一個翻轉開關復選框:
<div class="some-checkbox-area">
<input type="checkbox" data-role="flipswitch" name="flip-checkbox-lesson-complete"
data-on-text="Complete" data-off-text="Incomplete" data-wrapper-class="custom-size-flipswitch">
</div>
並發現解決方案是:
$("div.ui-page-active div.some-checkbox-area div.ui-flipswitch input[type=checkbox]").attr("checked", true).flipswitch( "refresh" )
筆記
div.ui-page
內容加載到單個 HTML 頁面中以提高性能。 因此,我從來沒有真正理解如果id
可能在 HTML body
中出現多次(也許有人可以澄清這一點),我怎么能使用它。prop
而不是attr
,則開關將進入無限翻轉循環! 我沒有進一步調查...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.