Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图将值设置为textbox-Jquery时返回的对象_Jquery_Ajax_Textbox - Fatal编程技术网

试图将值设置为textbox-Jquery时返回的对象

试图将值设置为textbox-Jquery时返回的对象,jquery,ajax,textbox,Jquery,Ajax,Textbox,我不熟悉使用Jquery,所以请原谅我的无知。 我已经从本地mysql数据中创建了一个动态列表,当用户选择列表中的一个值时,我希望所选的一些信息显示在textbox中。我成功地收集了我需要的信息,并将值设置为各种属性标签。然而,当我最终得到一个附加到文本框的值时,它会说 “[对象]” 在文本框中。我读过关于将其转换为文本的文章,但似乎无法在我的示例练习中准确地理解这一点。我想知道是否有人能帮我 呈现信息并将值设置到文本框的代码示例如下: <script type="text/jav

我不熟悉使用Jquery,所以请原谅我的无知。 我已经从本地mysql数据中创建了一个动态列表,当用户选择列表中的一个值时,我希望所选的一些信息显示在textbox中。我成功地收集了我需要的信息,并将值设置为各种属性标签。然而,当我最终得到一个附加到文本框的值时,它会说

“[对象]”

在文本框中。我读过关于将其转换为文本的文章,但似乎无法在我的示例练习中准确地理解这一点。我想知道是否有人能帮我

呈现信息并将值设置到文本框的代码示例如下:

    <script type="text/javascript">
    $(document).on('pagebeforeshow', '#index', function(){
        $("#list").empty();
        var url="http://localhost/test/login/json4.php";
        $.getJSON(url,function(json){
            //loop through deals
            $.each(json.deals,function(i,dat){
                $("#list").append("<li><a id='"+dat.dealid+"' data-restaurantid=" + dat.restaurantid + " data-image=" + dat.image + "><h1>"+dat.name+"</h1><p>"+dat.dname+"</p></a></li>");
                $(document).on('click', '#'+dat.dealid, function(event){  
                    if(event.handled !== true) // This will prevent event triggering more then once
                    {
                        dealObject.dealID = $(this).attr('id'); 
                        dealObject.restaurantid = $(this).attr('data-restaurantid');
                        dealObject.name = $(this).find('desc').eq(0).val(this);

                        $.mobile.changePage( "#index2", { transition: "slide"} );
                        event.handled = true;
                    }
                });            
            });
            $("#list").listview('refresh');
        });
    });

    $(document).on('pagebeforeshow', '#index2', function(){       

        $('#index2 [data-role="content"]').find('input#desc').val(dealObject.name);


    });

    var dealObject = {
        dealID : null,
        restaurantid : null,
        name : null,
        image : null,
        dname : null
    }    
</script>

$(文档).on('pagebeforeshow','#index',函数(){
$(“#列表”).empty();
变量url=”http://localhost/test/login/json4.php";
$.getJSON(url,函数(json){
//循环交易
$.each(json.deals,function(i,dat){
$(“#列表”)。追加(“
  • ”+dat.name+”“+dat.dname+”

  • ”); $(document).on('click','#'+dat.dealid,函数(事件){ if(event.handled!==true)//这将防止事件触发多次 { dealObject.dealID=$(this.attr('id'); dealObject.restaurantid=$(this.attr('data-restaurantid'); dealObject.name=$(this.find('desc').eq(0.val)(this); $.mobile.changePage(“#index2”,{transition:“slide”}); event.handled=true; } }); }); $(“#列表”).listview(“刷新”); }); }); $(document).on('pagebeforeshow','#index2',function(){ $('#index2[data role=“content”]').find('input#desc').val(dealObject.name); }); var对象={ dealID:null, restaurantid:null, 名称:空, 图像:空, dname:null }
    这就是我要展示它的地方:

    <form id="test">
            <label for="desc">Description</label>
            <input type="text" value="" name="desc" id="desc"/>  
            <a data-role="button" id="submit-button" data-theme="b">Submit</a>
    
    
    描述
    提交
    

    如果有人能帮我,我会非常感激。提前谢谢

    您正试图将文本
    val(this)
    this
    的值设置为
    中的对象

    如果你想得到价值,你可以说

     dealObject.name = $(this).find('desc').eq(0).val();
    
    如果您正在赋值

    dealObject.name = $(this).find('desc').eq(0).val('My Value');
    or 
    dealObject.name = $(this).find('desc').eq(0).val(this.something);
    
    所以你的代码

        <script type="text/javascript">
        $(document).on('pagebeforeshow', '#index', function(){
            $("#list").empty();
            var url="http://localhost/test/login/json4.php";
            $.getJSON(url,function(json){
                //loop through deals
                $.each(json.deals,function(i,dat){
                    $("#list").append("<li><a id='"+dat.dealid+"' data-restaurantid=" + dat.restaurantid + " data-image=" + dat.image + "><h1>"+dat.name+"</h1><p>"+dat.dname+"</p></a></li>");
                    $(document).on('click', '#'+dat.dealid, function(event){  
                        if(event.handled !== true) // This will prevent event triggering more then once
                        {
                            dealObject.dealID = $(this).attr('id'); 
                            dealObject.restaurantid = $(this).attr('data-restaurantid');
                            dealObject.name = $(this).find('desc').eq(0).val();
    
                            $.mobile.changePage( "#index2", { transition: "slide"} );
                            event.handled = true;
                        }
                    });            
                });
                $("#list").listview('refresh');
            });
        });
    
        $(document).on('pagebeforeshow', '#index2', function(){       
    
            $('#index2 [data-role="content"]').find('input#desc').val(dealObject.name);
    
    
        });
    
        var dealObject = {
            dealID : null,
            restaurantid : null,
            name : null,
            image : null,
            dname : null
        }    
    </script>
    
    
    $(文档).on('pagebeforeshow','#index',函数(){
    $(“#列表”).empty();
    变量url=”http://localhost/test/login/json4.php";
    $.getJSON(url,函数(json){
    //循环交易
    $.each(json.deals,function(i,dat){
    $(“#列表”)。追加(“
  • ”+dat.name+”“+dat.dname+”

  • ”); $(document).on('click','#'+dat.dealid,函数(事件){ if(event.handled!==true)//这将防止事件触发多次 { dealObject.dealID=$(this.attr('id'); dealObject.restaurantid=$(this.attr('data-restaurantid'); dealObject.name=$(this.find('desc').eq(0.val(); $.mobile.changePage(“#index2”,{transition:“slide”}); event.handled=true; } }); }); $(“#列表”).listview(“刷新”); }); }); $(document).on('pagebeforeshow','#index2',function(){ $('#index2[data role=“content”]').find('input#desc').val(dealObject.name); }); var对象={ dealID:null, restaurantid:null, 名称:空, 图像:空, dname:null }

    我希望这能有所帮助

    您的主要问题是dealObject中的name属性,name是一个特殊的词,不能用作对象属性名称


    在本例中,您将其添加到正确的输入中,但dealObject.name不存在。由于jQuery Mobile的工作方式,即使是空元素也会返回“[object]”

    Hi@Sedz谢谢您的回复。然而,我仍然会犯同样的错误。我已将.val()选项更改为一些不同的选项,但我似乎仍在输出“[Object,Object]”在jsfiddle.netSorry制作一个演示我实际上并不熟悉jsfiddle.net我正在本地主机上运行所有这些,所以我想我无法上传数据?