微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用JQuery从下拉列表中获取选定的值并将其保存在数据库中

如何解决使用JQuery从下拉列表中获取选定的值并将其保存在数据库中

添加Jquery代码之前,我能够将dropdownlist值保存到数据库

function Cartola() {
  var url = "https://api.cartolafc.globo.com/mercado/destaques";
  var response = UrlFetchApp.fetch(url);
  var data = response.getContentText();
  var result = JSON.parse(data);
  
  var apelido = result.Atleta[0].apelido;
  var foto = result.Atleta[0].foto;
  var clube_nome = result.Atleta[0].clube_nome;
  var posição = result.Atleta[0].posicao;
  
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear()
  var headerRow = ['apelido','foto','clube_nome','posição'];
  sheet.appendRow(headerRow);
  for(var i=0;i<result[0].Atleta;i++){
    var row = [result.Atleta[i].apelido,result.Atleta[i].foto,result.Atleta[i].clube_nome,result.Atleta[i].posicao];
    SpreadsheetApp.getActiveSheet().appendRow(row);
  }
}
我尝试了所有命令

此asp.net后端代码中的空引用错误

  <select asp-for="CategoriesId" id="CategoriesId" class="form-control" asp-items="ViewBag.CategoriesId">
                                        <option value="0">select</option>
                                    </select>
<script>
$(document).ready(function () {
    $("#CarMake").hide();
    $('#CategoriesId').on('change',function () {
        if (this.value == '2')           
        {
            $("#CarMake").show();
        }
        else {
            //$(this).val()
            //$('#CategoriesId').data();
           // $('#CategoriesId').val($(this).val());
            $('#CategoriesId').text();
        }
       
    });
});

解决方法

https://dotnetfiddle.net/nSMhi0

控制器和视图模型

public class MyCategory
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
}
public class FatimaViewModel
{
    public IList<MyCategory> CategoryList { get; set; }
    public int SelectedCategory { get; set; }
}
public class HomeController : Controller
{
    [HttpPost]
    public ActionResult Index17(FatimaViewModel vm)
    {
        //put breakpoint here
        var selectedItem = vm.SelectedCategory;
        FatimaViewModel vmOriginal = PopulateViewModel();
        return View(vmOriginal);
    }
    public ActionResult Index17()
    {
        FatimaViewModel vm = PopulateViewModel();
        return View(vm);
    }

查看

@model WebApplication2.Controllers.FatimaViewModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"
            integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
            crossorigin="anonymous"></script>
    <title>Index8</title>
    <script>
        $(document).ready(function () {
            $("#CarMake").hide();
            $('#CategoriesId').on('change',function () {
                if (this.value == '2') {
                    $("#CarMake").show();
                }
                //got rid of unnessary code
            });
        });
    </script>
</head>
<body>
    <div>
        <div>Select cateogry 2 to show text</div>
        <div id="CarMake">This is shown when second category selected</div>
        @using (Html.BeginForm())
        {
            @Html.DropDownListFor(r => r.SelectedCategory,new SelectList(Model.CategoryList,"CategoryId","CategoryName"),"--select--",new { id = "CategoriesId" })
            <input type="submit" value="Go" />
        }

    </div>
</body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。