简体   繁体   English

onchange中的onchange将不起作用

[英]onchange in onchange won't work

When I am building 2 dropdowns filling them from the database, the second dropdown is created when a value is picked from the first dropdown. 当我从数据库中构建2个下拉列表填充它们时,从第一个下拉列表中选择一个值时,将创建第二个下拉列表。 But then, when I select an option in the second, my ajax is being executed. 但是,当我第二次选择一个选项时,我的ajax正在执行。 But when I have only one value in the second dropdown, it won't work. 但是,当我在第二个下拉列表中只有一个值时,它将不起作用。 Even if I call the javascript function manualy. 即使我手动调用javascript函数。

The 2nd dropdown: 第二个下拉列表:

<?
include ('../dbconnect.php');
$query = "CALL get_projects(".$userid.",".$q.")";

$result = mysql_query($query);
$countprojects = mysql_num_rows($result);

if ($countprojects != 0){

    echo '<select class="form-control" onchange="showContent(this.value)">'."\n";
    if ($countprojects > 1){
        echo "<option value='none' selected>Select project</option>";
    }
    while($rowprojecten = mysql_fetch_assoc($result)){
        echo '<option value='.$rowprojecten['projectID'].'>'.$rowprojecten['projectname'].'</option>';
        $lastvalue = $rowprojecten['projectID']; // see below why i did this
    }

    echo '</select>';

?>

the javascript function I wrote/copied: 我编写/复制的javascript函数:

function showContent(str)
{
if (str=="")
  {
  document.getElementById("project").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("project").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","./includes/ajax/getcontent.php?q="+str,true);
xmlhttp.send();
}

The phpfile what is called from this javascript: phpfile从此javascript调用了什么:

<?
$q = intval($_GET['q']);

include ('../dbconnect.php');

$query = "CALL get_project(".$userid.",".$q.")";

$return = '<div id="project">';
$return .= $query;
$return .= '</div>';

echo $return;

mysql_close($con);

?>

Why do I see the div 'project' change when I select one, but does'nt it change when it is created? 为什么选择div时会看到div“项目”更改,但是创建后它没有更改?

I tried to call the function manualy with adding this to the first php file. 我试图手动调用该函数,并将其添加到第一个php文件中。 But it also doesn't work. 但这也不起作用。

if ($countprojects == 1){ 
  echo '<script type="text/javascript">
        showContent('.$lastvalue.')
        </script>';
}

sorry for my bad english, I hope you can help me solve this. 对不起,我的英语不好,希望您能帮助我解决这个问题。

This is because there is only one option in your second drop down list. 这是因为第二个下拉列表中只有一个选项。 you cannot fire change event if there is only on or zero options in drop down list. 如果下拉列表中只有一个或零个选项,则无法触发更改事件。 what you can do is add this code where your first Ajax call get fired when selection changes. 您可以做的就是添加此代码,以在选择更改时触发您的第一个Ajax调用。 and place your second Ajax call in inside below code. 并将您的第二个Ajax调用放在下面的代码中。

if ($('#selectproject').children().length == 1) {

            // make sure you have imported latest jquery. if not add this inside HTMl head  : <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
            var selectedProject = $('#selectproject').children()[0].value;
            showContent(selectedProject); // i hope this is the function you are calling when executing. if not please replace your function call here.
        }

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

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