[英]jQuery if input[type=“radio”] before clickable label hasn't attribute “checked” then do NOT add it, else do nothing
請幫助我實現此行為。 我想檢查單擊單選按鈕旁邊的標簽后,此單選按鈕是否已經具有“選中”屬性,那么就可以了,讓我們繼續這個階段。 但是,如果此元素沒有此屬性,那么我不會添加它。 這是我生的:
$.fn.hasAttr = function (name) {
return this.attr(name) !== undefined;
};
var lab = $(".rating input[type='radio'] + label");
lab.click(function {
if ($(this).lab.prev('input').hasAttr('checked')) {
alert("OK");
};
});
請幫助我。 提前致謝!
$('#ret_info_srars1 label').on('click', function(e) {
e.preventDefault();
if($(this).prev('input[type=radio]').is(':checked'))
alert("OK!");
});
確定是否在jQuery中選中了單選按鈕的正確方法是:
$("input:radio").is(':checked')
您還可以使用單選按鈕的change事件:
$('.rating input:radio').change(function(){
if ($(this).is(':checked'))
{
alert ("changed and checked");
}
});
看到它在擺弄
要設置要檢查的單選按鈕,請使用.prop
$("input:radio").prop("checked", true)
或者您可以只寫:
$(lab).is(':checked')
{
alert("Radio Button Checked");
}
因為實驗室已經分配了所需的ID。
要么
if($("#RadioButtonID).is(':checked'))
{
alert("Radio Button Checked");
}
else
{
alert ("Not Checked");
}
如果您的目標確實是按照上面的注釋所述創建一組“只讀”單選按鈕,則最簡單的解決方案是禁用單選按鈕:
$('input[type="radio"]').prop('disabled', true);
這是您更新的小提琴: http : //jsfiddle.net/voveson/25d3bja7/3/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.