使用 jquery 创建表单时的表单字段稳定性

Form fields stability when using jquery to create the form

本文关键字:表单 字段 稳定性 创建 使用 jquery      更新时间:2023-09-26

我正在使用json来填充选择标签的选项,当我选择国家时,系统会同步填充城市的组合框

<label class="AddressLable">الدوله</label>
                <select name="country"  id="countryCB">
                <option >اختر</option>
                <?php $cntr=0;      
                while($cntr < count($countries))
                {
                ?>
                    <option value="<?php echo $countries[$cntr]['countryNo']?>" <?php echo $_POST['country']==$countries[$cntr]['countryNo']?'selected':''?>> <?php echo $countries[$cntr]['countryName'];?></option>
                <?php 
                $cntr++;
                }
                ?>
                </select>
                <label class="AddressLable">المدينة</label>
                <select name="city" id="cityCB">
                </select>

这里就是jQuery脚本

$("#countryCB").change(function(){
    var countryNo=$(this).val();
    var param={"action":"getCities",
                "countryNo":countryNo
    };
    $.getJSON("controllers/Customer.controller.php",param,function(result){
        input=$("#cityCB");
        $('>option',input).remove();
        $("#cityCB").append('<option> اختر</option>');
        $.each(result,function(i,val){
        var option = $('<option />');
        option.attr("value",val.cityNo);
        option.text(val.cityName);
        $("#cityCB").append(option);
        })
    });

我怎么能做出这样的东西:

<?php echo $_POST['country']==$countries[$cntr]['countryNo']?'selected':''?>

在jquery?

$("cityCB option[value='your_value']").attr("selected", true);

在追加所有选项后运行它。