[英]Radio button REQUIRED if input field has content
我有一個相當快的問題要解決(我認為)。 我在線上有一個表格,它可以驗證用戶數據所需的內容,但是在表格的第一部分沒有驗證。
但是,有人問我是否可以根據輸入字段是否已填寫來使單選按鈕成為必需。
可以在這里找到該表格: http : //www.elcorteingles.pt/reservas/livros_escolares/form.asp
因此,如果此人開始填寫第一行中的輸入字段,則該組中的單選按鈕將變為必需(對於CDROM或CADERNO,但不能同時適用於兩者)
您可以處理focusout
和blur
的事件input
:
$(function () {
// Handle every input type text.
// To select specific inputs, give them a common class and change the
// selector accordingly.
$("input[type=text]").on("focusout blur", function () {
// Check for inputs with class radio_btns which are in
// the parent element (li).
// Set their required property.
$(this).parent().find("input.radio_btns")
.prop("required", $(this).val().trim().length > 0);
});
});
嘗試使用以下js代碼正常工作:
$(document).ready(function(){
$(".titulo_books").each(function(){
$(this).focus(function(){
var radioChecked=0;
var currElemId = parseInt($(this).attr('id'));
var radioSelecterId = (currElemId>9) ? currElemId : "0"+currElemId;
$("input:radio[name="+radioSelecterId+"caderno]").each(function(){
if(radioChecked==0)
{
radioChecked==1;
$(this).attr("checked","checked");
}
});
});
});
});
我已經通過從您站點上的控制台執行此命令進行了檢查,它似乎工作正常。 您可以按照自己的方式更改它。 我已經檢查了四個可用單選按鈕之一。 用戶可以根據需要更改輸入值。 或者,您也可以更改通過我的代碼選擇的默認單選按鈕。
這將起作用。 您可以在script標記中包含以下JQuery代碼,還可以在head標記中包含JQuery cdn鏈接。
$(document).ready(function(){
$('#01titulo').focusout(function(){
if ($(this).val() !== "") {
$('[name="01caderno"]').prop('required', true);
} else {
$('[name="01caderno"]').prop('required', false);
}
alert($('[name="01caderno"]').attr('required'));
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.