[英]How to check checkbox value in Jquery UI Dialog
我正在嘗試在JQuery UI對話框中調用changeShowTips()方法,但是該方法始終返回“未檢查!!!!”
似乎是什么問題?
<div id="dialog-tip">
<div>
<input type="checkbox" name="showTips" value="showTips" checked="checked" onclick="changeShowTips();"/>Show tips on start up
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
showTips()
});
function showTips() {
$("#dialog-tip").dialog({
height: 520,
width: 515,
modal: true,
}).parents('.ui-dialog:eq(0)').wrap('<div class="black-tie"></div>');
}
function changeShowTips() {
if (showTips.checked == true) {
alert('checked!!!');
}
else {
alert('NOT checked!!!');
}
}
</script>
您有一些凌亂的jQuery
<div id="dialog-tip">
<div>
<input type="checkbox" name="showTips" value="showTips" checked="checked"/>Show tips on start up
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
showTips()
$('input', '#dialog-tip').click(changeShowTips()); // do not have inline js, bind it here
});
function showTips() {
$("#dialog-tip").dialog({
height: 520,
width: 515,
modal: true,
}).parents('.ui-dialog').eq(0).wrap('<div class="black-tie"></div>');
// it is faster for sizzle to use a standard css selector to modify the jquery object and then use a jquery specific selector 'eq' separately
// instead of combining css/jquery selectors in one statement
}
function changeShowTips() {
if ($('input', '#dialog-tip').is(':checked')) { // there is jQuery for this - you should also compare and not assign
alert('checked!!!');
}
else {
alert('NOT checked!!!');
}
}
</script>
那是因為在您的代碼中, showTips
是指函數而不是目標元素。
<input type="checkbox"
name="showTips"
value="showTips"
checked="checked"
onclick="changeShowTips(this);"
/> Show tips on start up
function changeShowTips(showTips) {
if (showTips.checked) {
alert('checked!!!');
}
else {
alert('NOT checked!!!');
}
}
試試這個...
給輸入一個showTips的ID以及名稱,然后...
function changeShowTips() {
if ($('#showTips').is(':checked')) {
alert('checked!!!');
}
else {
alert('NOT checked!!!');
}
}
從語法上看,您似乎正在嘗試使用普通JS而不是jQuery來選中該復選框。
jQuery選擇器看起來像這樣的$([...]),通常您可以使用它來選擇類$(“。someclass”)或ID $(“#someid”)。 如果您確實不想為輸入字段提供ID,請查看文檔: http : //api.jquery.com/attribute-equals-selector/
然后,您可以使用$([...])。prop(“ checked”)來確定它是否被選中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.