在PHP中的JQuery的onchange问​​题

在PHP中的JQuery的onchange问​​题

本文介绍了在PHP中的JQuery的onchange问​​题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,标签的onchange需要更改并显示实际记录(例如,如果更改Men意味着男装记录将打开(显示)),我需要将选项值中的变量传递给它。



这里是我的Html代码:

 < select name =categoryid ='category'onchange ='gender(this)'style =background:transparent> 
< option id ='gender'hidden =hidden>性别< / option>
<?php foreach($ mens as $ row){?>
< option value =men> Boy's< / option>
< option value =girl>女孩的< / 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 +值:+ selectedValue);
});
});
< / script>

这里是我的PHP代码:

  $ men =SELECT * FROM`tbl_master_property` where status = 0; 
$ men_result = $ conn->查询($ men);
$ men_projects = array();
while($ row = mysqli_fetch_assoc($ men_result)){
$ men_projects [] = $ row;
}
$ mens = $ men_projects;
echo'< pre>';的print_r($男子);模具;

我打印我的$ mens它显示:

 数组

[0] =>数组

[pg_id] => 1
[名称] => Sri Manikanta男士新奢侈品付费嘉宾
[gender] => 0
[location_id] => 0


[1 ] =>阵列

[pg_id] => 2
[姓名] => Srivari新行政人员为男士支付嘉宾
[gender] => 0
[location_id] => 0


[2] =>数组

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


[3 ] =>阵列

[pg_id] => 4
[姓名] => Srinivasa Luxury Guest For Men
[gender] => 1
[location_id] => 0


解决方案

$ mens 数组中填充选项:

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

如果这不是您的意思,您可能需要澄清更多。






编辑1:

您可能希望使用ajax,但如果您的示例相对较小,则可以设想使用数组来绘制。



DEMO:

 < select name =categoryid ='category'style =background:transparent> 
< option id ='gender'hidden =hidden>性别< / option>
< option value =men>男士< / option>
< option value =girl>女士们< / option>
< / select>

< 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 '='+ v.gender +'>'+ v.name +'< / option>');
}
});

$('#项目')。html(opts.join(''));
});
});
< / script>






编辑2:



这是我最后一次猜测你想要的内容,我想你可能想重新加载页面但发送值选择:

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


Actually, onchange of tag need to change and display actual record for example (if change Men means mens record will open(display)) i need to pass variable inside option value how will pass.

Here my Html code:

<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>

Here my jquery code:

<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>

here with my Php code:

$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;

I print my $mens it is display :

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
        )
解决方案

I think you are trying to get the options to populate from the $mens array:

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

If this is not what you mean, you may have to clarify more.


EDIT 1:

If you have a large list of items to draw from, you will want to use ajax, but if your sample is relatively small, you can conceivably just use an array to draw from.

DEMO: https://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>


EDIT 2:

This is my last guess on what you want, from comments I think maybe you want to reload the page but send the the value selection:

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

这篇关于在PHP中的JQuery的onchange问​​题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:02