php中的jqueryonchange问题

00jrzges  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(176)

实际上,标签的一次更改需要更改并显示实际记录例如(如果更改人意味着男人记录将打开(显示))我需要传递选项值内的变量如何传递。
下面是我的html代码:

<select name="category" id ='category' onchange='gender(this)' style="background:transparent">
                  <option id ='gender' hidden="hidden">Gender</option>
                <?php foreach($mens as $row){?>
                  <option value="men">Boy's</option>
                  <option value="girl">Girl's</option>
                <?php }?>
              </select>

这里是我的jquery代码:

<script type="text/javascript">
    $(function () {
        $("#category").change(function () {
            var selectedText = $(this).find("option:selected").text();
            var selectedValue = $(this).val();
              $( "#list" ).submit();
            alert("Selected Text: " + selectedText + " Value: " + selectedValue);
        });
    });
</script>

下面是我的php代码:

$men ="SELECT * FROM `tbl_master_property` where status=0";
    $men_result=$conn->query($men);
     $men_projects = array();
        while($row=mysqli_fetch_assoc($men_result)){ 
        $men_projects[] = $row;
        }
         $mens = $men_projects; 
         echo '<pre>'; print_r($mens);die;

我打印我的$mens它是显示:

Array
(
    [0] => Array
        (
            [pg_id] => 1
            [name] => Sri Manikanta New Luxury Paying Guest For Men
            [gender] => 0
            [location_id] => 0

        )
    [1] => Array
        (
            [pg_id] => 2
            [name] => Srivari New Executive Paying Guest For Men
            [gender] => 0
            [location_id] => 0

        )
    [2] => Array
        (
            [pg_id] => 3
            [name] => Temple View New Executive Pg For Ladies
            [gender] => 1
            [location_id] => 0

        )
    [3] => Array
        (
            [pg_id] => 4
            [name] => Srinivasa Luxury Guest For Men
            [gender] => 1
            [location_id] => 0   
        )
y53ybaqx

y53ybaqx1#

我认为您正在尝试从 $mens 数组:

<?php foreach($mens as $row){?>
    <option value="<?php echo $row['gender'] ?>"><?php echo $row['name'] ?></option>
<?php }?>

如果这不是你的意思,你可能要澄清更多。
编辑1:
如果要从中绘制的项目列表很大,则需要使用ajax,但是如果示例相对较小,则可以使用数组进行绘制。
demo:httpshttp://jsfiddle.net/z50m5hnz/:

<select name="category" id ='category' style="background:transparent">
    <option id ='gender' hidden="hidden">Gender</option>
    <option value="men">Men's</option>
    <option value="girl">Ladies</option>
</select>

<select name="items" id="items">
<select>

<script type="text/javascript">
    var dropdown_items = <?php echo json_encode($mens) ?>;
    $(function () {
        $("#category").change(function () {
            var selectedText = $(this).find("option:selected").text();
            var selectedValue = $(this).val();
            var opts = [];
            $.each(dropdown_items,function(k,v){
                if(selectedValue == 'men' && v.gender == 0) {
                    opts.push('<option name="'+v.gender+'">'+v.name+'</option>');
                }
                else if(selectedValue == 'girl' && v.gender == 1) {
                    opts.push('<option name="'+v.gender+'">'+v.name+'</option>');
                }
            });

            $('#items').html(opts.join(''));
        });
    });
</script>

编辑2:
这是我最后一次猜测你想要什么,从评论我想也许你想重新加载页面,但发送值选择:

<script type="text/javascript">
    $(function () {
        $("#category").change(function () {
            var selectedValue = $(this).val();
            window.location =   '?select='+selectedValue;
        });
    });
</script>

相关问题