【问题标题】:Get selected value of <select> loaded by jquery获取 jquery 加载的 <select> 的选定值
【发布时间】:2015-10-26 12:20:18
【问题描述】:

我有一个 html 标记,并用 ajax 和 jquery 填充它更多

<option></option>

我的问题是:

当我试图与人相处时

jquery $("#select").val();

它总是返回“未定义”。

我也尝试使用 .text();和它一样。

这是我的 jquery/javascript 代码,用于填充选项选择下拉列表:

function CaricaDaTipo() {
         var dati = {nome : "recuperaCategorieAttributi", candidateFamiglie : $("#selectTipo").val()};
            $.ajax({
               type: "GET",
               url: "elaboraPhp.php",
               data: dati,
               error: function(data) {alert(data);},
               success: function(result) {
                    var arrayRitorno = eval (result);
                    $("#selectCategorie").empty();
                    for (var index in arrayRitorno)
                    {
                        if (index == 0) {
                            $("#selectCategorie").val(arrayRitorno[index]);
                        }
                        $("#selectCategorie").append($("<option value='"+arrayRitorno[index]+"'>"+arrayRitorno[index]+"</option>"));
                    }
                    RiempiDivConElementi();
               }
            }); 

在我的页面中我有 2 个选择:

1_ 标准项目使用 html 创建。当我改变这个时,一个函数被调用来用正确的选项填充第二个选择。 2_当我在第二个选择上选择项目时,我调用一个填充 div 的函数。

除了第二次选择的获取值之外,所有工作正常。

谁有这个问题的解决方案??

编辑:

这是我在 html 中的选择:

<label for="selectCategorie">Categoria:</label>
    <select id="selectCategorie" name="selectCategorie">
        <!-- CARICATO AUTOMATICAMENTE NON APPENA SI SCEGLIE IL TIPO -->
    </select>

解决方案:

我找到了解决办法:

在我看来,问题是当我尝试通过 jquery 的#selector 获取值但在 DOM 中它不存在,因为我在客户端生成它而不更改 DOM。

我改变这样的功能:

function CaricaDaTipo() {
     var dati = {nome : "recuperaCategorieAttributi", candidateFamiglie : $("#selectTipo").val()};
        $.ajax({
           type: "GET",
           url: "elaboraPhp.php",
           data: dati,
           error: function(data) {alert(data);},
           success: function(result) {
                var arrayRitorno = eval (result);
                var catSel = "";
                $("#selectCategorie").empty();
                for (var index in arrayRitorno)
                {
                    if (index == 0) {
                        $("#selectCategorie").val(arrayRitorno[index]);
                        catSel = arrayRitorno[index];
                    }
                    $("#selectCategorie").append($("<option value='"+arrayRitorno[index]+"'>"+arrayRitorno[index]+"</option>"));
                }
                RiempiDivConElementi(catSel);
           }
        }); 
}

function RiempiDivConElementi(cat) {
    var dati = {nome : "generaTabellaPermessiUtenti", scheda : $("#selectTipo").val(), categoria : cat, id : <?php echo($datiUtenteCaricati['ID']);?> };
    $.ajax({
           type: "GET",
           url: "elaboraPhp.php",
           data: dati,
           error: function(data) {alert(data);},
           success: function(result) {
                $("#permessiAttributiDiv").html("");
                $("#permessiAttributiDiv").html(result);
           }
        }); 
}



$(document).ready(function() {

   $("#selectTipo").on("change", function(){
        CaricaDaTipo();
   });

   $("#selectCategorie").on("change", function(){
        RiempiDivConElementi($(this).val());
   });

   $("#permessiDiv input").on("change", "input", function(){

    )};

   CaricaDaTipo();


});

现在我用 javascript 传递变量,而不用 jquery 找到它。 现在一切正常。

谢谢

【问题讨论】:

  • 您确定您的选择有id="select" 吗?
  • 您应该使用$("select").val();$("#selectCategorie").val();

标签: jquery html ajax select get


【解决方案1】:

如果您选择了 id='select',那么您的代码就是完美的。其他明智的写

$("select").val();

您编辑的问题答案 -

$("#selectCategorie").val();

【讨论】:

    【解决方案2】:

    您有错误的选择器来定位选择元素。元素的 ID 为 selectCategorie 而不是 selectselect 是下拉列表的标记名。因此你应该使用:

    $("#selectCategorie").val();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多