我使用jQuery來隱藏我的網站上的實際單選按鈕,並用圖像(自定義單選按鈕)替換它們。 jQuery允許用戶單擊圖像,該圖像依次「選擇」隱藏的單選按鈕。實際的單選按鈕在html代碼中附有onclick事件,但當我通過圖像(jQuery)「選擇」一個單選按鈕時,實際的單選按鈕不會檢測到選擇,因此onclick事件不是活性。使用jQuery選擇隱藏的單選按鈕,但onclick不工作
我可以確認onclick事件腳本在使用默認單選按鈕(無jQuery)時工作;當我使用jQuery作爲自定義單選按鈕時它不起作用。這是我到目前爲止的代碼:
HTML代碼
<div class="prettyRadiobuttons clearfix">
<ul id="store">
<li><input name="store" type="radio" id="store_1" value="store_1" onclick="dynamic_Select('code.php', this.value)" />
<label for="store_1">Store 1</label></li>
<li><input name="store" type="radio" id="store_2" value="store_2" onclick="dynamic_Select('code.php', this.value)" />
<label for="store_2">Store 2</label></li>
</ul>
</div>
的jQuery(自定義單選按鈕)
$(document).ready(function() {
//Adds the "radiolist" class to the div containing the radio buttons
$("div.prettyRadiobuttons").addClass("radiolist");
//Adds the html for the custom radio button (image) to each radio button (li)
$("div.radiolist li").append('<a class="radio-select" href="#">Select</a><a class="radio-deselect" href="#">Cancel</a>');
//Handles selecting a radio button
$(".radiolist .radio-select").click(
function(event) {
event.preventDefault();
var $boxes = $(this).parent().parent().children();
$boxes.removeClass("selected");
$(this).parent().addClass("selected");
$(this).parent().find(":radio").attr("checked","checked");
}
);
//Handles de-selecting a radio button
$(".radiolist .radio-deselect").click(
function(event) {
event.preventDefault();
$(this).parent().removeClass("selected");
$(this).parent().find(":radio").removeAttr("checked");
}
);
});
不同類別的jQuery的用於分配不同的圖像通過CSS根據是否選擇單選按鈕。
jQuery代碼無需使用onclick事件,onclick事件可以在不使用jQuery的情況下工作,但是我無法讓它們一起工作。任何幫助將非常感激!