【问题标题】:How to store drop down selected text in hidden field using jQuery如何使用jQuery在隐藏字段中存储下拉选定的文本
【发布时间】:2020-10-20 05:41:56
【问题描述】:

我是 Jquery 的新手,google artical 帮不了我。我知道这会很容易。任何方式因为我可以将下拉选定的值存储在隐藏的字段中,同样我想将选定的项目存储在另一个隐藏的字段中但无法执行相同的操作。 谁能帮帮我?

存储下拉值运行良好

$('#ddlLocation').change(function (e) {
                $('#hdnvalue').val(this.value);
            });

hdnvalue是我的隐藏字段ID,用于存储价值

存储所选文本不起作用:

 $('#ddlLocation').change(function (e) {
                $('#hdnvalue').val(this.value);
                $('#hdntext').val($("#ddlLocation option:selected").text()); 
            });

hdntext 是我另一个用于存储文本的隐藏文件 ID

【问题讨论】:

  • 你的代码应该可以工作,但你能在帖子中分享相关的 HTML 吗?也可以使用代码 sn -p 来重现它
  • @BhushanKawadkar 请检查我的更新代码
  • @saf 有什么错误请看控制台吗?
  • @JamshaidKamran 控制台没有错误
  • 那么这意味着您的事件没有绑定到您的控件

标签: c# jquery asp.net


【解决方案1】:

您好,我有一个简单的解决方法。

 <script type="text/javascript">
   
   $("#example").on("change",function(){
      $this = $("#example option:selected");
       
        $("#storeValue").val($this.val());
        $("#storeText").val($this.text());
       

   });
 </script>
 <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> 
   
 </head>
 

 <select id="example">
   <option value="one">one-text</option>
   <option value="two">two-text</option>
   <option value="three">three-text</option>
   <option value="four">four-text</option>
 </select>

 <input type="hidden" id="storeValue">
 <input type="hidden" id="storeText">

   $("#example").on("change",function(){
      $this = $("#example option:selected");
       
        $("#storeValue").val($this.val());
        $("#storeText").val($this.text());
       

   });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<select id="example">
   <option value="one">one-text</option>
   <option value="two">two-text</option>
   <option value="three">three-text</option>
   <option value="four">four-text</option>
 </select>

 <input type="hidden" id="storeValue">
 <input type="hidden" id="storeText">

【讨论】:

  • 我按照建议替换了我的代码 $("#storeText").val($this.text());显示错误 this.text 不是函数
  • 你好,你得这样写 $this = $("#storeText option:selected");
  • 另外请检查您的 jquery 版本
  • 或者你也可以使用 $this = $(this).find("option:selected"); likethis 希望你的问题现在解决了
【解决方案2】:

从 cmets 和描述中,我猜测 DropDownList ddlLocation 的 ID 正在被修改,因此您无法使用它绑定任何 jquery 事件。我建议像这样绑定DropDownList:

$('#<%= ddlLocation.ClientID %>').change(function (e) {
    $('#<%= hdnvalue.ClientID %>').val($(this).val());
    // DoStuff
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    相关资源
    最近更新 更多