Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript根据用户输入值隐藏/显示问题_Javascript_Jquery_Html - Fatal编程技术网

Javascript根据用户输入值隐藏/显示问题

Javascript根据用户输入值隐藏/显示问题,javascript,jquery,html,Javascript,Jquery,Html,当用户选择某些值时,我试图隐藏和/或显示表单元素。 例如,如果用户对同意问题选择“是”,我需要它显示一些问题,但是,如果用户更改对同意问题的回答,我需要它隐藏这些问题 这是我到目前为止的想法 $(document).ready(function () { var input = document.getElementById('consent'); var consent_responses = [{ "0": hideConsent },{ "1": showConsent

当用户选择某些值时,我试图隐藏和/或显示表单元素。 例如,如果用户对同意问题选择“是”,我需要它显示一些问题,但是,如果用户更改对同意问题的回答,我需要它隐藏这些问题

这是我到目前为止的想法

$(document).ready(function () { 
    var input = document.getElementById('consent');
    var consent_responses = [{ "0": hideConsent },{ "1": showConsent }];
    input.addEventListner('click', function () {
        var consent_response;
        if (consent_responses[this.value]) {
            content_response = consent_responses[this.Function()]
        }
        else {
            content_response = consent_responses[this.Function]
        }
    });

    function showConsent(){
        $("#date").show(),
        $("#referrer").show(),
        $("#f_name").show(),
        $("#phone_num").show(),
        $("#leave_msg").show(),
        $("#email").show(),
    };
    function hideConsent(){
        $("#date").hide(),
        $("#referrer").hide(),
        $("#f_name").hide(),
        $("#phone_num").hide(),
        $("#leave_msg").hide(),
        $("#email").hide(),
    }; });

在这里拉小提琴:

这可以简化:

var input = document.getElementById('consent');
// Let's use the value as key, and the functions as values
var consent_responses = {
    "0" : hideConsent,
    "1" : showConsent
};
input.addEventListener("click", function () {
    // Get the appropriate function given the value, and invoke it
    consent_responses[this.value]();
});

function hideConsent() {
    // ...
}

function showConsent() {
    // ...
}

这可以简化:

var input = document.getElementById('consent');
// Let's use the value as key, and the functions as values
var consent_responses = {
    "0" : hideConsent,
    "1" : showConsent
};
input.addEventListener("click", function () {
    // Get the appropriate function given the value, and invoke it
    consent_responses[this.value]();
});

function hideConsent() {
    // ...
}

function showConsent() {
    // ...
}

这可以简化:

var input = document.getElementById('consent');
// Let's use the value as key, and the functions as values
var consent_responses = {
    "0" : hideConsent,
    "1" : showConsent
};
input.addEventListener("click", function () {
    // Get the appropriate function given the value, and invoke it
    consent_responses[this.value]();
});

function hideConsent() {
    // ...
}

function showConsent() {
    // ...
}

这可以简化:

var input = document.getElementById('consent');
// Let's use the value as key, and the functions as values
var consent_responses = {
    "0" : hideConsent,
    "1" : showConsent
};
input.addEventListener("click", function () {
    // Get the appropriate function given the value, and invoke it
    consent_responses[this.value]();
});

function hideConsent() {
    // ...
}

function showConsent() {
    // ...
}

你可以这样做:

基本上,我只修复了一些拼写错误(在发布之前您是否真的尝试了代码?),并在单选按钮中添加了事件侦听器

document.getElementById(...).addEventListener(...)

我还为您的单选按钮提供了唯一的ID。

您可以这样做:

基本上,我只修复了一些拼写错误(在发布之前您是否真的尝试了代码?),并在单选按钮中添加了事件侦听器

document.getElementById(...).addEventListener(...)

我还为您的单选按钮提供了唯一的ID。

您可以这样做:

基本上,我只修复了一些拼写错误(在发布之前您是否真的尝试了代码?),并在单选按钮中添加了事件侦听器

document.getElementById(...).addEventListener(...)

我还为您的单选按钮提供了唯一的ID。

您可以这样做:

基本上,我只修复了一些拼写错误(在发布之前您是否真的尝试了代码?),并在单选按钮中添加了事件侦听器

document.getElementById(...).addEventListener(...)

我还为您的单选按钮提供了唯一的ID。

最好将您的问题(需要隐藏的)用类封装在div中。隐藏的“或样式”显示:无;“。并在需要时通过简单地询问divshow()或hide()来简化代码

像这样:

<form id="screening">
<div class="col-md-12 col-sm-12 col-xs-12 nopad" id="create">
    <div class="form-group text-center">
        <b>Do you agree to answer the screening questions?</b><br />
        <div class="radio" id="consent">
            <label>
                <input type="radio" name="consent" id="consent" value="1">
                Yes, I consent
            </label>
        </div><br />
        <div class="radio">
            <label>
                <input type="radio" name="consent" id="consent" value="0">
                No, I do not consent
            </label>
        </div>
    </div>

<!-- simplify by using this -->
<div id="questions" style="display: none;">

    <div class="form-group" id="date">
        <label for="date">What is today's date?</label>
        <input type="date" class="form-control" id="date" name="date" />
    </div>
    <div class="form-group" id="referrer">
        <label for="referrer">How did you hear about us/our studies?</label>
        <select class="form-control" name="referrer" id="referrer">
            <option></option>
            <option value="1">Flyers</option>
            <option value="2">Print Media</option>
            <option value="3">A friend</option>
            <option value="4">Online (e.g., Craigslist)</option>
            <option value="5">Other</option>
        </select>
    </div>
    <div class="form-group" id="other_explain">
        <label for="other_explain">Please specify other source.</label>
        <textarea class="form-control" rows="3" id="other_explain" name="other_explain"></textarea>
    </div>
    <div class="form-group" id="f_name">
        <label for="f_name">What is your first name?</label>
        <input type="text" class="form-control" id="f_name" name="f_name" />
    </div>
    <div class="form-group" id="phone_num">
        <label for="phone_num">What is a phone number at which you can be contacted?   </label>
        <input type="tel" class="form-control" id="phone_num" name="phone_num" />
    </div>
    <div class="form-group" id="leave_msg">
        <label for="leave_msg">If we call and you are not available, may we leave a message?</label><br />
        <div class="radio">
            <label>
                <input type="radio" name="leave_msg" id="leave_msg" value="1">
                Yes
            </label>
        </div><br />
        <div class="radio">
            <label>
                <input type="radio" name="leave_msg" id="leave_msg" value="0">
                No
            </label>
        </div>
    </div>
    <div class="form-group" id="email">
        <label for="email">What is an e-mail at which you can be contacted?</label>
        <input type="email" class="form-control" id="email" name="email" />
    </div>
</div>
</div>
</form>
您使用:

function showConsent(){
        $("#questions").show(),
    };

最好用类将你的问题(需要隐藏的问题)封装在div中。隐藏的“或样式”显示:无;“。并在需要时通过简单地询问divshow()或hide()来简化代码

像这样:

<form id="screening">
<div class="col-md-12 col-sm-12 col-xs-12 nopad" id="create">
    <div class="form-group text-center">
        <b>Do you agree to answer the screening questions?</b><br />
        <div class="radio" id="consent">
            <label>
                <input type="radio" name="consent" id="consent" value="1">
                Yes, I consent
            </label>
        </div><br />
        <div class="radio">
            <label>
                <input type="radio" name="consent" id="consent" value="0">
                No, I do not consent
            </label>
        </div>
    </div>

<!-- simplify by using this -->
<div id="questions" style="display: none;">

    <div class="form-group" id="date">
        <label for="date">What is today's date?</label>
        <input type="date" class="form-control" id="date" name="date" />
    </div>
    <div class="form-group" id="referrer">
        <label for="referrer">How did you hear about us/our studies?</label>
        <select class="form-control" name="referrer" id="referrer">
            <option></option>
            <option value="1">Flyers</option>
            <option value="2">Print Media</option>
            <option value="3">A friend</option>
            <option value="4">Online (e.g., Craigslist)</option>
            <option value="5">Other</option>
        </select>
    </div>
    <div class="form-group" id="other_explain">
        <label for="other_explain">Please specify other source.</label>
        <textarea class="form-control" rows="3" id="other_explain" name="other_explain"></textarea>
    </div>
    <div class="form-group" id="f_name">
        <label for="f_name">What is your first name?</label>
        <input type="text" class="form-control" id="f_name" name="f_name" />
    </div>
    <div class="form-group" id="phone_num">
        <label for="phone_num">What is a phone number at which you can be contacted?   </label>
        <input type="tel" class="form-control" id="phone_num" name="phone_num" />
    </div>
    <div class="form-group" id="leave_msg">
        <label for="leave_msg">If we call and you are not available, may we leave a message?</label><br />
        <div class="radio">
            <label>
                <input type="radio" name="leave_msg" id="leave_msg" value="1">
                Yes
            </label>
        </div><br />
        <div class="radio">
            <label>
                <input type="radio" name="leave_msg" id="leave_msg" value="0">
                No
            </label>
        </div>
    </div>
    <div class="form-group" id="email">
        <label for="email">What is an e-mail at which you can be contacted?</label>
        <input type="email" class="form-control" id="email" name="email" />
    </div>
</div>
</div>
</form>
您使用:

function showConsent(){
        $("#questions").show(),
    };

最好用类将你的问题(需要隐藏的问题)封装在div中。隐藏的“或样式”显示:无;“。并在需要时通过简单地询问divshow()或hide()来简化代码

像这样:

<form id="screening">
<div class="col-md-12 col-sm-12 col-xs-12 nopad" id="create">
    <div class="form-group text-center">
        <b>Do you agree to answer the screening questions?</b><br />
        <div class="radio" id="consent">
            <label>
                <input type="radio" name="consent" id="consent" value="1">
                Yes, I consent
            </label>
        </div><br />
        <div class="radio">
            <label>
                <input type="radio" name="consent" id="consent" value="0">
                No, I do not consent
            </label>
        </div>
    </div>

<!-- simplify by using this -->
<div id="questions" style="display: none;">

    <div class="form-group" id="date">
        <label for="date">What is today's date?</label>
        <input type="date" class="form-control" id="date" name="date" />
    </div>
    <div class="form-group" id="referrer">
        <label for="referrer">How did you hear about us/our studies?</label>
        <select class="form-control" name="referrer" id="referrer">
            <option></option>
            <option value="1">Flyers</option>
            <option value="2">Print Media</option>
            <option value="3">A friend</option>
            <option value="4">Online (e.g., Craigslist)</option>
            <option value="5">Other</option>
        </select>
    </div>
    <div class="form-group" id="other_explain">
        <label for="other_explain">Please specify other source.</label>
        <textarea class="form-control" rows="3" id="other_explain" name="other_explain"></textarea>
    </div>
    <div class="form-group" id="f_name">
        <label for="f_name">What is your first name?</label>
        <input type="text" class="form-control" id="f_name" name="f_name" />
    </div>
    <div class="form-group" id="phone_num">
        <label for="phone_num">What is a phone number at which you can be contacted?   </label>
        <input type="tel" class="form-control" id="phone_num" name="phone_num" />
    </div>
    <div class="form-group" id="leave_msg">
        <label for="leave_msg">If we call and you are not available, may we leave a message?</label><br />
        <div class="radio">
            <label>
                <input type="radio" name="leave_msg" id="leave_msg" value="1">
                Yes
            </label>
        </div><br />
        <div class="radio">
            <label>
                <input type="radio" name="leave_msg" id="leave_msg" value="0">
                No
            </label>
        </div>
    </div>
    <div class="form-group" id="email">
        <label for="email">What is an e-mail at which you can be contacted?</label>
        <input type="email" class="form-control" id="email" name="email" />
    </div>
</div>
</div>
</form>
您使用:

function showConsent(){
        $("#questions").show(),
    };

最好用类将你的问题(需要隐藏的问题)封装在div中。隐藏的“或样式”显示:无;“。并在需要时通过简单地询问divshow()或hide()来简化代码

像这样:

<form id="screening">
<div class="col-md-12 col-sm-12 col-xs-12 nopad" id="create">
    <div class="form-group text-center">
        <b>Do you agree to answer the screening questions?</b><br />
        <div class="radio" id="consent">
            <label>
                <input type="radio" name="consent" id="consent" value="1">
                Yes, I consent
            </label>
        </div><br />
        <div class="radio">
            <label>
                <input type="radio" name="consent" id="consent" value="0">
                No, I do not consent
            </label>
        </div>
    </div>

<!-- simplify by using this -->
<div id="questions" style="display: none;">

    <div class="form-group" id="date">
        <label for="date">What is today's date?</label>
        <input type="date" class="form-control" id="date" name="date" />
    </div>
    <div class="form-group" id="referrer">
        <label for="referrer">How did you hear about us/our studies?</label>
        <select class="form-control" name="referrer" id="referrer">
            <option></option>
            <option value="1">Flyers</option>
            <option value="2">Print Media</option>
            <option value="3">A friend</option>
            <option value="4">Online (e.g., Craigslist)</option>
            <option value="5">Other</option>
        </select>
    </div>
    <div class="form-group" id="other_explain">
        <label for="other_explain">Please specify other source.</label>
        <textarea class="form-control" rows="3" id="other_explain" name="other_explain"></textarea>
    </div>
    <div class="form-group" id="f_name">
        <label for="f_name">What is your first name?</label>
        <input type="text" class="form-control" id="f_name" name="f_name" />
    </div>
    <div class="form-group" id="phone_num">
        <label for="phone_num">What is a phone number at which you can be contacted?   </label>
        <input type="tel" class="form-control" id="phone_num" name="phone_num" />
    </div>
    <div class="form-group" id="leave_msg">
        <label for="leave_msg">If we call and you are not available, may we leave a message?</label><br />
        <div class="radio">
            <label>
                <input type="radio" name="leave_msg" id="leave_msg" value="1">
                Yes
            </label>
        </div><br />
        <div class="radio">
            <label>
                <input type="radio" name="leave_msg" id="leave_msg" value="0">
                No
            </label>
        </div>
    </div>
    <div class="form-group" id="email">
        <label for="email">What is an e-mail at which you can be contacted?</label>
        <input type="email" class="form-control" id="email" name="email" />
    </div>
</div>
</div>
</form>
您使用:

function showConsent(){
        $("#questions").show(),
    };


也许添加html并制作一个小提琴?如果你想要一个快速的答案,这会很有帮助……你能提供一个包含HTML的JSFIDLE吗?也许可以添加HTML并制作一个FIDLE?如果你想要一个快速的答案,这会很有帮助……你能提供一个包含HTML的JSFIDLE吗?也许可以添加HTML并制作一个FIDLE?如果你想要一个快速的答案,这会很有帮助……你能提供一个包含HTML的JSFIDLE吗?也许可以添加HTML并制作一个FIDLE?如果你想要一个快速的答案,它会很有帮助……你能提供一个完整的JSFIDLE和HTML吗?做了更改,外观更好,函数与问题相同做了更改,外观更好,函数与问题相同做了更改,外观更好,函数与问题相同做了更改,外观更好,与问题相同的函数我确实喜欢此方法。。。但是我有一个愚蠢的问题,当将输入的值保存到数据库时,php使用的名称不是正确的id?提交表单后,您的服务器将使用
-标记的名称处理数据,是的。为了能够正确地寻址元素(客户端),您应该始终在HTML中使用唯一的ID。谢谢,我只需要从其他人那里听到这一点来验证。我喜欢你的解决方案。把这个带到下一步。如果用户在填写2个问题后单击“否我不同意”,当用户单击“否我不同意”时,我将如何重置所有值?您可以添加类似于
document.getElementById('date')。value=''(用于文本输入元素)。你必须确保你有唯一的ID。现在代码中有几个元素叫做“日期”。我很喜欢这个方法。。。但是我有一个愚蠢的问题,当将输入的值保存到数据库时,php使用的名称不是正确的id?提交表单后,您的服务器将使用
-标记的名称处理数据,是的。为了能够正确地寻址元素(客户端),您应该始终在HTML中使用唯一的ID。谢谢,我只需要从其他人那里听到这一点来验证。我喜欢你的解决方案。把这个带到下一步。如果用户在填写2个问题后单击“否我不同意”,当用户单击“否我不同意”时,我将如何重置所有值?您可以添加类似于
document.getElementById('date')。value=''(用于文本输入元素)。你必须确保你有唯一的ID。现在代码中有几个元素叫做“日期”。我很喜欢这个方法。。。但是我有一个愚蠢的问题,当将输入的值保存到数据库时,php使用的名称不是正确的id?提交表单后,您的服务器将使用
-标记的名称处理数据,是的。为了能够正确地寻址元素(客户端),您应该始终在HTML中使用唯一的ID。谢谢,我只需要从其他人那里听到这一点来验证。我喜欢你的解决方案。把这个带到下一步。如果用户在填写2个问题后单击“否我不同意”,当用户单击“否我不同意”时,我将如何重置所有值?您可以添加类似于
docum的内容