简体   繁体   English

文本/字符串下拉值无法填写

[英]text/string drop-down values cannot be filled

I am wondered why its happening. 我不知道为什么会这样。

Actually, I am using dropdown based textbox selection. 实际上,我正在使用基于下拉列表的文本框选择。

whenever my drop-down option value I am using numbers then textbox filled properly based on my drop-down selection. 每当我的下拉选项值使用数字时,文本框将根据我的下拉选择正确填充。

Although I am using my dropdown value text/string then textbox values cannot be filled when I change my drop-down selection. 尽管我使用下拉列表值text / string,但是更改下拉列表选择时无法填充文本框值。

I want drop-down option value text/string then textbox value filled.pls, help a lot. 我想要下拉选项值文本/字符串,然后文本框值fill.pls,有很大帮助。

Working Code:- 工作代码:

 <select id="sel_depart">
    <option value="0">- Select -</option>
    <option value="1">too</option>
    <option value="2">Stencil</option>
    </select>

 <input id="sel_user" size="">

Jquery jQuery的

<script type="text/javascript">
    $(document).ready(function(){

        $("#sel_depart").change(function(){
            var deptid = $(this).val();

            $.ajax({
                url: 'getUsers2.php',
                type: 'post',
                data: {depart:deptid},
                dataType: 'json',
                success:function(response){

                    var len = response.length;

                    $("#sel_user").empty();
                    for( var i = 0; i<len; i++){
                        var id1234 = response[i]['id123'];
                        //var name1234 = response[i]['name123'].children('option:selected').text();
                        var name1234 = response[i]['name123'];
                       // $("#sel_user").append("<option value='"+id+"'>"+name+"</option>");

                      // var value = $(this).children('option:selected').text();

                      $("#sel_user").val(name1234);

                     // $("#sel_user").val(value);

                     // $.isNumeric($("#sel_user").val(name1234));

                    }
                }
            });
        });

    });
</script>  

getUsers2.php getUsers2.php

<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "boti_leave_info"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());
}

$departid = $_POST['depart'];   // department id

$sql = "SELECT ID,Mobile FROM user_content WHERE Category=".$departid;

$result = mysqli_query($con,$sql);

$users_arr = array();

while( $row = mysqli_fetch_array($result) ){
    $userid = $row['ID'];
    $name = $row['Mobile'];
    $users_arr[] = array("id123" => $userid, "name123" => $name);
}

// encoding array to json format
echo json_encode($users_arr);
?>

The same code is not working when i just change my <option value="too">too</option> instead of <option value="1">too</option> 当我只是更改我的<option value="too">too</option>而不是<option value="1">too</option>时,相同的代码不起作用

listen for the change event on the sel_depart and set the value of the input accordingly : 侦听sel_depart上的change事件,并相应地设置输入的value

 $(document).ready(function(){ $('#sel_depart').on('change', function(){ //var value = $(this).val(); // uncomment this line to get the value (0, 1, 2 ... ) var value = $(this).children('option:selected').text(); $('#sel_user').val( value ); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="sel_depart"> <option value="0">- Select -</option> <option value="1">too</option> <option value="2">Stencil</option> </select> <input id="sel_user" size=""> 

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

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