[英]Unable to send 2 values to a javascript function from onchange event of select box
I am trying to write a code where in I am dynamically generating select box options. 我正在尝试编写代码,在其中动态生成选择框选项。 I need to send 2 values from this onchange event.
我需要从此onchange事件发送2个值。 1 is the sub_header_id and other is the header_id.
1是sub_header_id,其他是header_id。 I need to send $row["header_id"] to the javascript function.
我需要将$ row [“ header_id”]发送到javascript函数。 Following is my PHP code.
以下是我的PHP代码。 How do I do that?
我怎么做? I don't want to send a concatenated string and then split it in javascript function.
我不想发送串联的字符串,然后在javascript函数中将其拆分。
<?php
include('config.php');
$sql = sprintf("SELECT * FROM retail_sub_headers");
$result = mysql_query($sql);
$num = mysql_num_rows($result);
if ($num > 0)
{
echo "<form>";
echo "<select id = \"my_select_box\" onChange = \"javascript:load_brand_option_box(this, 'populate_brands', '');\">";
echo "<option selected value = \"-------\">-------</option>";
while($row = mysql_fetch_array($result))
{
echo "<option value = ".$row["sub_header_id"].">".$row["sub_header_name"]."</option>";
}
echo "</select>";
echo "</form>";
}
?>
And here is my javascript function javascript:load_brand_option_box. 这是我的JavaScript函数javascript:load_brand_option_box。
function load_brand_option_box (sub_header_id, action, brand_id)
{
var sub_header_value = sub_header_id.options[sub_header_id.selectedIndex].value
if (action == "populate_brands")
{
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("brand_names").innerHTML=xmlhttp.responseText;
//document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","populate.php?action="+action+"&sub_header_id="+sub_header_value+"&brand_id="+"",true);
xmlhttp.send();
}
}
Try this.... Added a newval
attribute in option for header id
you can now grab
header id
inside the function
尝试一下。...在
header id
选项中添加了newval
属性,您现在可以在function
内grab
header id
<?php
include('config.php');
$sql = sprintf("SELECT * FROM retail_sub_headers");
$result = mysql_query($sql);
$num = mysql_num_rows($result);
if ($num > 0)
{
echo "<form>";
echo "<select id = \"my_select_box\" onChange = \"load_brand_option_box(this, 'populate_brands', '');\">";
echo "<option selected value = \"-------\">-------</option>";
while($row = mysql_fetch_array($result))
{
echo "<option value = ".$row["sub_header_id"]." newval=".$row["header_id"].">".$row["sub_header_name"]."</option>";
}
echo "</select>";
echo "</form>";
}
?>
function load_brand_option_box (sub_header_id, action, brand_id)
{
var sub_header_value = sub_header_id.options[sub_header_id.selectedIndex].value
var sel = document.getElementById('my_select_box');
var header_id=sel.options[sel.selectedIndex].getAttribute('newval');
//you will get the header id here
if (action == "populate_brands")
{
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("brand_names").innerHTML=xmlhttp.responseText;
//document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","populate.php?action="+action+"&sub_header_id="+sub_header_value+"&brand_id="+"",true);
xmlhttp.send();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.