dowbwrr3590709 2016-04-19 20:50
浏览 21
已采纳

防止被选中的<option> -tag被.html()覆盖

I have a PHP file that fetches rows of car brands from a database, echoes these as "<option value=\"$brand\">" . $brand . "</option>"; and puts them inside pre-written <select>tags.

My issue is that the first item that appears in the select box is not passing its value onwards.

The value of the select box is changed by this event

$('select[name=model]').on('change', function() {
  selectedModel = $("#select-model").val()
});

The <option>-tags are generated by this loop in brands.php:

while ($row = mysqli_fetch_array($result)) {
  $brand = $row['brand'];
  echo "<option value=\"$brand\">" . $brand . "</option>";
}

The brands are fetched by this function:

function fetchBrands() {
  $.ajax({
    type: "POST",
    url: "script/rent/brands.php",
    data: {dateFrom: selectedDateFrom, 
           dateTo: selectedDateTo, 
           destination: selectedDestination},

    success: function(data) {
        $("#select-brand").html(data);
      }
  });
}

Because the data is posted to #select-brandwith .html() I can't set a default value for the select box because it gets overwritten. Appending the options will result in duplicates etc. as fetchBrands() is dependent on a previous set of radio buttons and select boxes.

  • 写回答

1条回答 默认 最新

  • douluhaikao93943 2016-04-19 20:59
    关注

    What I'd now suggest is that in your success function, add some code that gets the first element in the select tag, updates the selectedBrand, and then triggers the code that's attached to the change event. If you refactored your code so that you added a reference to the handler code, this would make it easier.

    $('select[name=model]').on('change', someFunction());
    
    function someFunction(){
        selectedModel = $("#select-model").val()
    }
    
    function fetchBrands() {
      $.ajax({
        type: "POST",
        url: "script/rent/brands.php",
        data: {dateFrom: selectedDateFrom, 
               dateTo: selectedDateTo, 
               destination: selectedDestination},
    
        success: function(data) {
            $("#select-brand").html(data);
            $("#select-brand").val($("#select-brand option:first").val());
            someFunction();
          }
      });
    }
    

    Before extra information was added:

    To have an element of a <select>...</select> automatically selected on page render, you need to add the attribute selected="selected" to the <option /> you want to be selected.

    Alternatively, add a hook for document load that sets the selected brand.

    Or, have the first option as something like this:

    <option disabled="disabled" selected="selected">Choose a Brand</option>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化