簡體   English   中英

在“單擊” jQuery時遇到麻煩

[英]Having trouble with jQuery “on” click

我試圖在單擊任一單選按鈕時觸發功能,但似乎無法正常工作。

我的代碼:

$('.radio [name=list_in]').on('click', function() {
    updateListingForm('list_in');
});

我的HTML:

<div class="fields">
    <div class="radio">
        <div class="pretty_rb styledRadio" style="background-image: url('images/form-radio-icons.png'); width: 19px; height: 20px; cursor: pointer; background-position: 0px -20px;"><input type="radio" checked="" value="auction" class="pretty_rb" id="list_site" name="list_in" style="display: none;"></div><label for="list_site">Site</label>
    </div>
    <div class="radio">
        <div class="pretty_rb styledRadio" style="background-image: url('images/form-radio-icons.png'); width: 19px; height: 20px; cursor: pointer; background-position: 0px 0px;"><input type="radio" value="store" class="pretty_rb" id="list_store" name="list_in" style="display: none;"></div><label for="list_store">Store</label>
    </div>                    
    <div class="contentClear"></div>
</div>

我使用的是jQuery插件來設置無線電字段的樣式,這是它輸出的HTML,因此這就是隱藏常規無線電字段的原因。

由於這個原因我無法使用onclick事件; 這就是為什么我嘗試on此處使用on的原因on但似乎無法啟動它?

我究竟做錯了什么!?

將偵聽器分配給呈現的元素,而不是隱藏的元素:

$('.radio .pretty_rb').on('click', function() {
    updateListingForm('list_in');
});

嘗試這個,

$('.radio').on('click', function() {
    updateListingForm('list_in');
});

我想您會錯過$(document).ready(function(){...});

該代碼對我有用:

$(document).ready(function() {
    $('.radio [name=list_in]').on('click', function() {
        alert('It works');
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM