简体   繁体   English

如何仅在选择时隐藏/显示div输入启用验证选项?

[英]How to enable validation of select option hide/show div-input only when it selected?

I have a select option box that when I select it, a div-input appear. 我有一个选择选项框,当我选择它时,将显示一个div输入。 The problem that when I put validation on the this div-input, and when i submit even when it hide, it require me to validate it, thus giving me error msg. 问题是,当我在此div输入上进行验证时,即使隐藏时我也提交了该问题,它要求我对其进行验证,从而给我错误消息msg。

How to do enable validation for this input field only when if it appear? 仅当此输入字段出现时,如何启用验证?

PHP 的PHP

<div class="form-group">
 <label for="inquiry_type">Subject *: </label>
<select name="type" id="inquiry_type" class="form-control">
     <option value="0">Please Select</option>
     <option value="test1">test1</option>
   <option value="test2">test2</option>
   <option value="others">Other...</option> //div-input appear when sel
 </select>
</div>

 <div id="other_subj" class="form-group">
    <label for="other_msg_subj">Input Subject Title *: </label>
       <input type="text" id="other_msg_subj" class="form-control" name="other_msg_subj" placeholder="Enter Subject Title" required/>
</div>

JQUERY for hide/show 用于隐藏/显示的JQUERY

<script>

$(function() {
    $('#other_subj').hide();
    $('#inquiry_type').change(function(){
        if($('#inquiry_type').val() === 'others') {
            $('#other_subj').show();
        } else {
            $('#other_subj').hide();
        }
    });
});

var inquiry_other_subj = document.getElementById("other_msg_subj").value;

 ...
 } else if (!inquiry_other_subj.match(/^(?=.*[A-Za-z ]).{5,20}$/)) {

   alert("Need to enter a proper inquiry title containing only letter!"); // I want this to be active only when the div is shown

 } else {

var parameters="...+"&other_msg_subj="+inquiry_other_subj;

PHP validation PHP验证

//Same for this php validation
...
else if(!preg_match ('%^[A-Za-z\.\' \-]{2,30}$%', $_POST['other_msg_subj'])){

    echo "Please enter a valid message subject title!";
    exit();

}

First, you need to disable the input when it's hidden to prevent sending it's data to the server: 首先,您需要在隐藏输入时将其禁用,以防止将其数据发送到服务器:

$(function() {
    $('#other_subj').hide();
    $('#inquiry_type').change(function(){
        if($('#inquiry_type').val() === 'others') {
            $('#other_msg_subj').prop('disabled', false);
            $('#other_subj').show();
        } else {
            $('#other_msg_subj').prop('disabled', 'disabled');
            $('#other_subj').hide();
        }
    });
});

And you need to do a check before validating: 并且您需要在验证之前进行检查:

On Javascript side: 在Javascript方面:

else if (
    $("#other_msg_subj").is(":visible") &&
    !inquiry_other_subj.match(/^(?=.*[A-Za-z ]).{5,20}$/)) {

On PHP side: 在PHP方面:

else if(isset($_POST['other_msg_subj']) && 
    !preg_match ('%^[A-Za-z\.\' \-]{2,30}$%', $_POST['other_msg_subj'])){

This may help. 这可能会有所帮助。

in JQUERY : JQUERY中

Validate only if others is selected. 仅在选择others选项时验证。

var inquiry_type = $('#inquiry_type').val();
else if (inquiry_type === 'others' && !inquiry_other_subj.match(/^(?=.*[A-Za-z ]).{5,20}$/)) {
   alert("Need to enter a proper inquiry title containing only letter!");
 }

Add the other_msg_subj parameter in the variable parameters only if others is selected. 仅在选择others参数的情况下,在变量parameters添加other_msg_subj参数。

var parameters = "......"; //Other parameters
if(inquiry_type == "others")
{
     parameters = parameters + "&other_msg_subj="+inquiry_other_subj;
}

in PHP : PHP中

Check if only the parameter is present 检查是否仅存在参数

else if(!isset($_POST['other_msg_subj']) && !preg_match ('%^[A-Za-z\.\' \-]{2,30}$%', $_POST['other_msg_subj']))
{
    echo "Please enter a valid message subject title!";
    exit();
}

Also (Just a suggestion) you can simplify the JQUERY code : 另外(只是一个建议),您可以简化JQUERY代码:

show/hide as toggle(true/false) true = show(), false = hide() show/hidetoggle(true/false) true = show(), false = hide()

$(function() {
    $('#other_subj').hide();
    $('#inquiry_type').change(function(){
        var display = $('#inquiry_type').val() === 'others';
        $('#other_subj').toggle(display);
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在选择 select 选项时显示 div 并在取消选择时隐藏? - How to show a div when a select option is selected and hide when unselected? "选择选择选项时如何显示隐藏的 div?" - How can I show a hidden div when a select option is selected? 下面的“隐藏/显示分区”取决于是否选择了一个选项 - Hide/Show Div below select depending on whether an option is selected or not 如何制作 <div> 仅在某个 <select>下拉选项被选中?该<select>用ng-repeat指令填充选项 - How to make a <div> show only when a certain <select> dropdown option is selected? The <select> options are populated with the ng-repeat directive 显示隐藏所选选项触发第二个选择选项然后第二个选择选项触发隐藏显示div - show hide the selected option trigger 2nd select option then 2nd select option trigger hide show div .html() 如何仅显示 select 的选定选项? - .html() how to show only selected option of select? 当在另一个选择中选择一个选项时,是否可以显示/隐藏某个选项并显示/隐藏其他选项? - Is there a way to show/hide some option and show/hide others in a select when an option is selected in a different select? 在多选选项中选择其他选项时显示输入字段 - Show input field when other option is selected in multiple select option 仅在多重选择中选择了特定选项时才显示输入字段 - Show input field only if a specific option is selected in multiple select 如何根据所选选项的ID显示/隐藏div? - how to show/hide div based on selected option's id?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM