简体   繁体   English

如果输入字段包含内容,则需要单选按钮

[英]Radio button REQUIRED if input field has content

I have aa reasonably quick problem to solve (I think). 我有一个相当快的问题要解决(我认为)。 I have a form online and it validates the required content for the user's data, but has no validation on the first part of the form. 我在线上有一个表格,它可以验证用户数据所需的内容,但是在表格的第一部分没有验证。

I've been asked however if I can make a radio button REQUIRED depending on whether an input field has been filled in. 但是,有人问我是否可以根据输入字段是否已填写来使单选按钮成为必需。

The form can be found here: http://www.elcorteingles.pt/reservas/livros_escolares/form.asp 可以在这里找到该表格: http : //www.elcorteingles.pt/reservas/livros_escolares/form.asp

So if the person start's filling in the input fields on the first line, that the radio buttons in the group become REQUIRED (for either the CDROM ou CADERNO but not both) 因此,如果此人开始填写第一行中的输入字段,则该组中的单选按钮将变为必需(对于CDROM或CADERNO,但不能同时适用于两者)

You can handle the focusout and blur events for the input : 您可以处理focusoutblur的事件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);
    });
});

Demo 演示版

jQuery reference (Tree Traversal) jQuery参考(树遍历)

jQuery reference (.prop()) jQuery参考(.prop())

jQuery reference (.focusout()) jQuery参考(.focusout())

jQuery reference (.blur()) jQuery参考(.blur())

Try using the following js code its working: 尝试使用以下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");
        }
        });


      });
    });

});        

I have checked it by executing this from console on your site and it seems to work fine. 我已经通过从您站点上的控制台执行此命令进行了检查,它似乎工作正常。 You can alter this in the way you want. 您可以按照自己的方式更改它。 I have checked one of the four available radio button. 我已经检查了四个可用单选按钮之一。 User can change the input value if required. 用户可以根据需要更改输入值。 Or you can also change the default radio button selected through my code. 或者,您也可以更改通过我的代码选择的默认单选按钮。

This will work. 这将起作用。 You can include the following JQuery code in the script tag, and also the JQuery cdn link in the head tag. 您可以在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM