[英]Can anyone help me how to get value using javascript?
I'm new to javascript that why i don't know how to get value using javascript. 我是javascript的新手,为什么我不知道如何使用javascript获取价值。
Here is my PHP script 这是我的PHP脚本
while($row = mysql_fetch_array($sql)){
echo "<div class='loop'>";
if($row['choice'] == 0){
echo "<p>First Choice</p>";
echo "<input type='hidden' id='hidden' value='0' />";
}
elseif($row['choice'] == 1){
echo "<p>Second Choice</p>";
echo "<input type='hidden' id='hidden' value='1' />";
}
else{
echo "<p><input type='radio' name='content_type' value='1' />This is A</p>
<p><input type='radio' name='content_type' value='2' />This is B</p>
<p><input type='radio' name='content_type' value='3' />This is C</p>";
//content_a
echo "<div id='content_a'>";
echo "<p>First Choice</p>";
echo "<input type='hidden' id='hidden' value='0' />";
echo "</div>";
//content_b
echo "<div id='content_b'>";
echo "<p>Second Choice</p>";
echo "<input type='hidden' id='hidden' value='1' />";
echo "</div>";
//content_c
echo "<div id='content_c'>";
echo "<p>Third Choice</p>";
echo "<input type='hidden' id='hidden' value='2' />";
echo "</div>";
}
echo "<a style='text-decoration: none; color: #000000;' class='alert'
href='#' onclick='return false;'>Alert</a>";
echo "</div>";
}
Javascript 使用Javascript
//when click on alert button
$(".alert").bind('click', function(){
var type = $(this).parents('.loop').find("#hidden").val();
alert(type);
});
//when radio button change
$('input[name=content_type]').bind('change', function(){
var n = $(this).val();
switch(n)
{
case '1':
$(this).parents('.loop').find('#content_a').show(1000);
$(this).parents('.loop').find('#content_b').hide(1000);
$(this).parents('.loop').find('#content_c').hide(1000);
break;
case '2':
$(this).parents('.loop').find('#content_b').show(1000);
$(this).parents('.loop').find('#content_a').hide(1000);
$(this).parents('.loop').find('#content_c').hide(1000);
break;
case '3':
$(this).parents('.loop').find('#content_c').show(1000);
$(this).parents('.loop').find('#content_a').hide(1000);
$(this).parents('.loop').find('#content_b').hide(1000);
break;
}
});
My javascript script above work only when choice == 0 and choice == 1 (when I click alert button with choice == 0 , it will alert 0, and click alert button with choice == 1 , it will alert 1). 我的javascript脚本仅在选项== 0和选择== 1时工作 (当我单击警报按钮时, 选项== 0 ,它将警告0,然后单击警报按钮, 选择== 1 ,它将警报1)。 But if choice == 2 , it didn't work at all, it alerts undefined.
但是如果选择== 2 ,它根本不起作用,它会发出未定义的警报。 I want when choice == 3 and user choose the radio button, it will do like below:
我想当选择== 3并且用户选择单选按钮时,它将如下所示:
- if user choose **This is A** and click Alert button, it will alert 0
- if user choose **This is B** and click Alert button, it will alert 1
- if user choose **This is C** and click Alert button, it will alert 2
Can you help me to solve this problem? 你能帮我解决这个问题吗?
Thank in advance 预先感谢
You have logic for 你有逻辑
if($row['choice'] == 0){
and 和
elseif($row['choice'] == 1){
but not in the same manner if the choice is 2. 如果选择是2,则不一样。
You also have three inputs with id='hidden.' 你还有三个id ='hidden'的输入。 Is it possible that the following line is finding the wrong hidden element?
以下行是否有可能找到错误的隐藏元素?
var type = $(this).parents('.loop').find("#hidden").val();
A live example in a site like jsfiddle.net would help us tinker more easily. 像jsfiddle.net这样的网站中的实例会帮助我们更轻松地修补。
you are given same id's in the below code. 您在下面的代码中给出了相同的ID。 id's need to given unique name.
id需要给出唯一的名称。
echo "<div id='content_a'>";
echo "<p>First Choice</p>";
echo "<input type='hidden' id='hidden' value='0' />";
echo "</div>";
//content_b
echo "<div id='content_b'>";
echo "<p>Second Choice</p>";
echo "<input type='hidden' id='hidden' value='1' />";
echo "</div>";
//content_c
echo "<div id='content_c'>";
echo "<p>Third Choice</p>";
echo "<input type='hidden' id='hidden' value='2' />";
echo "</div>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.