在单独的输入字段中显示jquery值

时间:2014-08-21 11:41:35

标签: jquery html

我试图将下面代码中的#response值显示在输入字段中。这是代码。

<script>
        $(document).ready(function() {

            $("#productID").change(function() {
                //alert($('#productID option:selected').val());
                var pId = $('#productID').val();

                $.get('updateProduct', {
                    productID: pId.trim()    //using trim to get rid of any extra invisible character from pId                    
                },
                function(responseText) {
                  $('#response').val(responseText);                       

                });

            });
        });
    </script>

$( '#响应')VAL(responseText的)。 - &GT;如果这包含两个值,那么有没有办法分别检索这些值?我想要做的是将值分别显示为三个单独的输入字段。

 $('#response').val(responseText);

这可能包含诸如产品描述,产品评论,产品类型等值。 这些值来自servlet。 收到这些值后,它们将显示在三个单独的输入字段中。

目前,如果我执行以下操作,

<input type="text" id="response"/>

这将在同一输入字段中显示所有三个值。 知道如何在三个独立的输入字段中显示三个值?提前致谢

输入来自servlet页面。在“doGet”中,我有以下代码。

 for (Product8339384 ps1 : ps) {
                comments = ps1.getComments();
                description =ps1.getDescription();                    
            }              
            out.println(comments);  
            out.println(description);

输出: 输出应显示在两个单独的输入字段中。这将显示评论和说明。这两个值都来自数据库。

2 个答案:

答案 0 :(得分:1)

我建议你遍历结果(ResponsText)并将数据(希望你有一个分隔符?)分成ex。 Javascript Array []。

如果您有办法知道数组中有多少项,您可以参考。使用JQuery动态插入输入元素,如下所示:

$(InputsWrapper).append('<div><input type="text" name="mytext[]" id="field_'+     FieldCount +'" value="Text '+ FieldCount +'"/></div>');

这样,您可以使用从Array获得的每个项目并插入一个新的Input元素,并在$ .each(JQuery)或for循环(Javascript)中为其分配数据。

如果你不想要临时数组方式,你也可以像这样遍历你的结果:

$.each(responseText.split(','), function(index, value) { 
  alert(index + ': ' + value); 
});

....鉴于你有什么要分开:)

希望这能为你提供更多的工作......

答案 1 :(得分:1)

这是解决方案。 Jquery代码位于标题内。

 $(document).ready(function() {
            $("#productID").change(function() {                   
                var pId = $('#productID').val();
                $.get('updateProduct', {
                    productID: pId.trim()    //using trim to get rid of any extra invisible character from pId                    
                },
                function(responseText) {                       
                    $.each(responseText.split(','), function(index, value) {
                        if(index === 0){
                          $('#comments').val(value);  
                        }
                        else if(index === 1){                                
                             $('#description').val(value);                                 
                        } 
                    });

                });
            });
        });

然后在html体内。

<input type="text" id="comments" name="id"/>
<input type="text" id="description" name="id"/>
相关问题