简体   繁体   English

根据先前的输入选项选择(单选按钮)显示div的内容。 在一个文件中工作,不在其他文件中工作

[英]Showing content of div based on previous input option choice (radio button). Working in one file, not in other

I have a two step form. 我有两步表格。 The first step asks a question and has 3 radio buttons. 第一步问一个问题,有3个单选按钮。

Based on which radio button is selected, the second step shows the answer by 'unhiding' a div. 根据选择哪个单选按钮,第二步通过“取消隐藏” div显示答案。

For eg Step 1: What option do you select? 例如, 步骤1:您选择什么选项? Option 1, Option 2, Option 3 选项1,选项2,选项3

Step 2: Has 3 hidden divs, based on the option in step 1, one of them is changed from display:none, to display:block and is shown. 步骤2:根据步骤1中的选项,具有3个隐藏的div,其中之一从display:none更改为display:block并显示出来。

Here's my code: 这是我的代码:

$(document).ready(function () {
$("input[rel$='lastq']").click(function () {
var value = $(this).val();
if (value == 'last1') {
$(".thank1").show();
$(".thank2").hide();
$(".thank3").hide();
}               
else if (value == 'last2') {
$(".thank2").show();
$(".thank1").hide();
$(".thank3").hide();
} 
else if (value == 'last3') {
$(".thank3").show();
$(".thank1").hide();
$(".thank2").hide();
}
else {
$(".thank1").hide();
$(".thank2").hide();
$(".thank3").hide();
}
});
$(".thank1").hide();
$(".thank2").hide();
$(".thank3").hide();
});

And my html is: 而我的html是:

<input type="radio" rel="lastq" name="" value="last1" id="" /> Option 1
<input type="radio" rel="lastq" name="" value="last2" id="" /> Option 2
<input type="radio" rel="lastq" name="" value="last3" id="" /> Option 3

<div class="thank1">thank you 1</div>
<div class="thank2">thank you 2</div>
<div class="thank3">thank you 3</div>

Now the weird thing is this is working fine with a single file (locally), the correct 'thank you' div shows. 现在奇怪的是,使用单个文件(本地)可以正常工作,正确的“谢谢” div显示。 But when I use it (locally again) in one my project (using wordpress on local machine), it does not work. 但是,当我在一个项目(在本地计算机上使用wordpress)中(再次在本地)使用它时,它不起作用。

Observed problem: 观察到的问题:

1) When it works, the "thank you divs" are by default style="display:none;" 1)工作正常时,“谢谢div”默认为style =“ display:none;”。

2) But when used in the project, when not working, the thank you divs do not have the display:none assigned to them. 2)但是当在项目中使用时,当不工作时,谢谢divs没有display:none分配给他们。

What could be causing this? 是什么原因造成的? Why isn't it getting assigned in the project, but still working in the standalone file? 为什么没有在项目中分配它,但仍在独立文件中工作?

PROBLEM SOLVED by adding $ = jquery.noconflict() at the top of the script code. 通过在脚本代码顶部添加$ = jquery.noconflict()解决了问题。

The jquery was loading fine, but it must be conflicting with some other script maybe? jQuery加载正常,但可能与其他脚本冲突吗?

Anyway, issue solved for anyone coming here later... 无论如何,问题解决了以后再来这里的人...

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

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