dongli7870 2016-09-21 16:59
浏览 98
已采纳

Ajax返回值与下拉列表的值匹配并显示SELECTED值。 腓

What I am doing is passing data via Ajax and putting that data into a Dropdown. So when the data gets passed it should match the selected value in the dropdown and then appear on top with SELECTED on the option so we can see the data right away. So if the data being returned is Product2 then Product2 should be SELECTED and visable.

<script type="text/javascript">
    function myFunction() {
        var skuvalue=document.my_form.modlistbox.value;
        $.ajax({ url: 'modify.php',
            data: {sku: skuvalue},
            type: 'post',
            dataType: "html",
            error: function (request, error) {
                console.log(arguments);
                alert("ERROR: " + error);
            },
            success: function(data) {
                var jsonvalue = $.parseJSON(data);
                console.log(jsonvalue);
                $(document).ready;
                $("#modsku").val(jsonvalue[0]);
                $("#modproduct").val(jsonvalue[1]).prop('selected', true); // I tried this and not working. Nothing is being SELECTED.
            }
        });
    };
</script>

<input type="text" name="modsku" id="modsku" maxlength="10">
<select name="modproduct" id="modproduct">
    <option></option>
    <option value="Product1">Product1</option>
    <option value="Product2">Product2</option>
    <option value="Product3">Product3</option>
</select>

For what I am looking to have done is something like this.

<select name="modproduct" id="modproduct">
    <option></option>
    <option value="Product1">Product1</option>
    <option value="Product2" selected>Product2</option>
    <option value="Product3">Product3</option>
</select>
  • 写回答

1条回答 默认 最新

  • dqlxtv1452 2016-09-21 17:11
    关注

    The chained function call here is superfluous (and doesn't really make much sense in the context, trying to set a selected property on a select element):

    $("#modproduct").val(jsonvalue[1]).prop('selected', true);
    

    You can set the selected option in a select simply by setting the value with jQuery:

    $("#modproduct").val(jsonvalue[1]);
    

    Note: There are other things in this code which don't make any sense. For example, this:

    $(document).ready;
    

    It doesn't do anything, and is really just an opportunity for an error. So there may very well be other problems in your code which prevent the overall result from happening.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建