简体   繁体   English

如何在第一个 select 框的基础上自动填充第二个 select 框并在第二个 select 框的基础上自动填充文本框

[英]How to auto populate second select box on base of first select box and auto populate textbox on base of second select box

product_listing.php FORM part product_listing.php FORM 部分

<?php 
$arr_cat_bn = [category01, category02];
$arr_subcat_ln_for_cbo_default = [subcategory01, subcategory02]; // For category01
$arr_subcat_ln_for_cbo_default = [subcategory11, subcategory12]; // For category02

$str_cat_bn_default = $arr_cat_bn[0];
if(isset($_POST["cbo_cat_bn"]))
{
    $str_cat_bn_default = trim($_POST["cbo_cat_bn"]);
}

$str_subcat_ln_form = $arr_subcat_ln_for_cbo_default[0];
if(isset($_POST["cbo_subcat_ln"]))
{
        $str_subcat_ln_form = trim($_POST["cbo_subcat_ln"]);
}
?>

<form role="form" name="frm_add" method="POST" action="product_listing.php">

    <select name="cbo_cat_bn" id="cbo_cat_bn" onChange="get_subcat_ln(this.value);">
        <?php 
        foreach($arr_cat_bn as $cat_bn) { 
            if($str_cat_bn_default == $cat_bn) { $str_selected="selected"; } else { $str_selected = ""; } ?>
            <option value="<?php echo $cat_bn; ?>" <?php print($str_selected);?>><?php echo $cat_bn; ?></option>
    </select>   
                
    <div id="before_get_subcat_ln">
        <select name="cbo_subcat_ln" id="cbo_subcat_ln"  onChange="get_qty(this.value);">
            <?php 
            sort($arr_subcat_ln_for_cbo_default);
            foreach($arr_subcat_ln_for_cbo_default as $subcat_ln) { 
            if(trim($str_subcat_ln_form) == trim($subcat_ln))  { $str_selected_ln = "selected"; } else { $str_selected_ln = ""; } 
            ?>
            <option value="<?php echo $subcat_ln; ?>" <?php print($str_selected_ln);?>><?php echo $subcat_ln; ?></option>
        </select>
    </div>
    <div id="after_get_subcat_ln"></div>    
                        
    <div id="before_get_qty">
        <input type="text" id="txt_qty" name="txt_qty" value="<?php print($int_qty); ?>">
    </div>
    <div id="after_get_qty"></div>    
               
    <button type="submit" >SUBMIT</button>                              
                        
</form>

product_listing.php jQuery part product_listing.php jQuery 零件

<script>
function get_subcat_ln(catbn) {
var str_catbn = catbn;

    var dataString = "cat_bn_all="+str_catbn;
    $.ajax({
        type: "POST",
        url: "../user/get_subcategory_p.php?",
        data: dataString,
        success: function(html)
        {
            $("#before_get_subcat_ln").hide();
            $("#after_get_subcat_ln").html(html);
        }
    });
}

function get_qty(subcatln) {
var str_subcatln = subcatln;

    var dataString = "cat_bn_all_and_subcat_ln="+str_subcatln;
    $.ajax({
        type: "POST",
        url: "../user/get_qty_p.php?", 
        data: dataString,
        success: function(html)
        {
            $("#before_get_qty").hide();
            $("#after_get_qty").html(html);
        }
    });
}
</script>

get_subcategory_p.php (auto populates sub category select box on base of category select box) get_subcategory_p.php(在类别 select 框的基础上自动填充子类别 select 框)

On this page, POST data processed and result data are stored in Array arr_subcat_ln and returned back to PHP page like this.在这个页面上,POST 处理的数据和结果数据存储在数组arr_subcat_ln中,并像这样返回到 PHP 页面。

echo "<select name='cbo_subcat_ln' id='cbo_subcat_ln'>";
sort($arr_subcat_ln);
foreach($arr_subcat_ln as $subcat_ln) { 
    echo "<option value='" . $subcat_ln . "'>" . $subcat_ln . "</option>";
} 
echo "</select>";

get_qty_p.php (auto populates qty text box on base of sub category select box) get_qty_p.php(在子类别 select 框的基础上自动填充数量文本框)

On this page, POST data processed and result data are stored in variable str_PackageSize and returned back to PHP page like this.在这个页面上,POST 数据处理和结果数据存储在变量str_PackageSize中,并像这样返回到 PHP 页面。

echo "<input type='text' id='txt_qty' name='txt_qty'  value='".$str_PackageSize."'>";
  • first select box = category select box第一个 select 框 = 类别 select 框
  • second select box = sub category select box第二个 select 框 = 子类别 select 框

When page loads, first value of category array displays in first select box by default.页面加载时,类别数组的第一个值默认显示在第一个 select 框中。 And based on default category in first select box, correct sub category values populates in second select box where first value of sub category array displays first by default.并且基于第一个 select 框中的默认类别,正确的子类别值填充在第二个 select 框中,其中子类别数组的第一个值默认首先显示。 After that correct qty displays in qty text box based on default sub category in second select box.之后,根据第二个 select 框中的默认子类别,在 qty 文本框中显示正确的数量。 If I change sub category from second select box, it gives correct qty value for that selected sub category.如果我从第二个 select 框更改子类别,它会为所选子类别提供正确的数量值。

Now, problem is that when I change value in first select box, it gives correct sub category values in second select box but doesn't make any change in qty text box on basis of default sub category value in second select box.现在,问题是,当我在第一个 select 框中更改值时,它会在第二个 select 框中给出正确的子类别值,但不会根据第二个 Z99938282F0407EFCF42Z 框中的默认子类别值在数量文本框中进行任何更改。 Now further if I change sub category value in second select box, still it doesn't change value of qty text box on basis of selected sub category value.现在,如果我在第二个 select 框中更改子类别值,它仍然不会根据所选子类别值更改 qty 文本框的值。

So qty is displayed correctly for default selected category and default selected sub category when page loads.因此页面加载时默认选择的类别和默认选择的子类别正确显示数量。 It gives correct qty if I don't change category and only change sub category.如果我不更改类别而只更改子类别,它会提供正确的数量。 Once I change category, I get correct sub category values for this selected category.更改类别后,我将获得此选定类别的正确子类别值。 But then I don't get correct qty for any sub category.但是我没有得到任何子类别的正确数量。

So please let me know that what wrong I am doing in my code or what is missing?所以请让我知道我在代码中做错了什么或缺少什么?

As the subcategory select-box is created dynamically you need to call get_qty(subcatln) under success function of ajax and pass the default value of subcategory which is available in html .由于子类别选择框是动态创建的,因此您需要在 ajax 的成功 function 下调用get_qty(subcatln)并传递子类别的默认值,该值在html中可用。 Now, to pass default value you can use two ways.They are as follows:现在,要传递默认值,您可以使用两种方式。它们如下:

First way : You can pass that html string in selector ie: $() and then use find(":first-child").val() which will give you first option of select-box and then you can pass same to your get_qty(subcatln) function.第一种方式:您可以在选择器中传递html字符串,即: $() ,然后使用find(":first-child").val()这将为您提供选择框的第一个选项,然后您可以将其传递给您的get_qty(subcatln) function。

Demo Code :演示代码

 var html = '<select name="cbo_subcat_ln" id="cbo_subcat_ln"><option value="first">first</option><option value="second">second</option><option value="third">third</option></select>' var subcatln = $(html).find(":first-child").val(); console.log("First default value: "+subcatln); //call function //get_qty(subcatln)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Second way : You can check if the div after_get_subcat_ln has any select inside it or not and depending on this you can get value of select and pass the same to your get_qty(subcatln) .第二种方式:您可以检查 div after_get_subcat_ln中是否有任何 select ,根据此您可以获得 select 的值并将其传递给您的get_qty(subcatln)

Demo Code :演示代码

 var html = '<select name="cbo_subcat_ln" id="cbo_subcat_ln"><option value="first">first</option><option value="second">second</option><option value="third">third</option></select>' $("#after_get_subcat_ln").html(html); //check if the div have `cbo_subcat_ln` if ($("#after_get_subcat_ln").find("select#cbo_subcat_ln").length > 0) { //get that value var subcatln = $("select#cbo_subcat_ln:first-child").val() console.log("Second way: " + subcatln) //call function //get_qty(subcatln); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="after_get_subcat_ln"></div>

You need to put above code in ajax success of get_subcat_ln(catbn) function.ie:你需要把上面的代码放在ajax成功get_subcat_ln(catbn) function.ie中:

function get_subcat_ln(catbn) {
  ...
    $.ajax({
        type: "POST",
        url: "../user/get_subcategory_p.php?",
        data: dataString,
        success: function(html)
        {
            $("#before_get_subcat_ln").hide();
            $("#after_get_subcat_ln").html(html);
          //here you need to get value of either way
          //call get_qty(subcatln) to update qty input
        }
    });
}

This is actually a JS issue.这实际上是一个JS问题。 You forgot to add the onChange in your php-script.您忘记在 php 脚本中添加onChange So whenever your <select> gets replaced the onChange is missing.因此,每当您的<select>被替换时, onChange就会丢失。

 $('#btn').on('click', function() { $('#select').html('<select><option>A</option><option>B</option>'); }); $('#btn2').on('click', function() { $('#select').html('<select onChange=\'alert("yay" + this.value);\'><option>A</option><option>B</option>'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="select"> <select> <option>1</option> <option>2</option> </select> </div> <hr /> <button id="btn">Load list without onChange</button><br /> <button id="btn2">Load list with onChange</button>

You may want to read this too: MCVE您可能也想阅读此内容: MCVE

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

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