简体   繁体   English

任何人都可以帮助我如何使用JavaScript获取价值?

[英]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.

相关问题 谁能帮我将此论文转换为javascript吗? - Can anyone help me convert this paperscript to javascript? 谁能帮助我进行JavaScript表单验证? - Can anyone help me with JavaScript form validation? 任何人都可以在 javascript 中帮助我删除按钮 - Anyone can help me with in javascript remove button 有人可以帮我解决 Javascript 的问题吗? - can anyone help me for the issue of Javascript post call is sending null value to mongoDB? 谁能帮我告诉我为什么我在函数中得到一个空值 - Can anyone help and tell me why do i get a null value in the function 任何人都可以帮助我使用 for 循环将诸如本示例之类的表格转换为 Javascript 中的动态表格吗? - Can anyone help me convert a table such as this example into a dynamic one in Javascript by using the for loop? 谁能帮我解释使用Selenium Webdriver进行图像验证的javascript代码吗? - Can anyone help me explaining javascript code for image validation using selenium webdriver? 任何人都可以帮助我理解这一点吗? 原型对象中的 JavaScript &#39;splice&#39; 方法 - Can anyone help me understand this? JavaScript 'splice' method in prototype object 在JavaScript中循环 - 任何可以帮助我理解的人? - for loop in JavaScript - anyone who can help me understand? 任何人都可以帮助我解决 javascript 中 var 的这种奇怪行为吗? - Can anyone help me with this weird behaviour of var in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM