【问题标题】:How to select value of a particular dropdown in a given row with multiple dropdown options?如何在具有多个下拉选项的给定行中选择特定下拉列表的值?
【发布时间】:2016-09-20 09:40:14
【问题描述】:

我有许多表单,有两个下拉选项名称和值,我希望选择名称选项作为数组中的一个变量,就像这样。

for (i = 0;i<noOfElements;i++)
           {
                var resultHtml = theModule.getHtml("rowformtemplate", {});
            //load html of form with fields **name** and **value** as dropdown
                $(section).append(resultHtml);
            //append html to a div
                var optValue = namearray[i];
            //from list of optons this value should be selected
                var form = $(section).find(".row-template");
            //find all forms in the section
                var row= form[form.length-1];
            //select latest form
            //How to select name field "name" of current row and set it to be optvalue which is a variaable something like this. but it selects name field of all form and sets it to optValue 
                $("select[name^='name'] option[value=" + optValue + "]").attr("selected","selected");
            } 

【问题讨论】:

  • 几行后我停止阅读,请发布一些代码和适当可读的段落。

标签: javascript jquery


【解决方案1】:

在您的最终 jquery 选择器中,您正在查看完整的 html 文档而不是最新的表单。

试试下面的

$(row).find("select[name='name'] option[value='" + optValue + "']").attr("selected","selected");

或者如果使用最新的 jquery

$(row).find("select[name='name'] option[value='" + optValue + "']").prop("selected",true);

同时检查 How do I select item with class within a DIV in JQuery

How do you select a particular option in a SELECT element in jQuery?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多