使用javascript提交表单时如何获取“文本”而不​​是“值” [英] how to get 'text' instead of 'values' when submitt a form using javascript

查看:79
本文介绍了使用javascript提交表单时如何获取“文本”而不​​是“值”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Javascript来获取用户输入表单的所有值。我也使用数据库填充级联下拉列表(国家,州,城市),并有他们的值和文本。我的问题是,当用户点击提交按钮时,我得到国家,国家,城市的值,如10,101,1011,但我想要像USA,Carolina,C1这样的文字。我的代码如下:

I am using Javascript to get those all values that user enters into the form. Also i have used database to fill the cascading dropdownlist(Country,States,City) and have thier values as well as text. My question is that when user clicks submit button i get the values of 'Country','States','City' like 10,101,1011 but i want text like USA,Carolina,C1 . My code is as follows:

function callall()
      {
          var Name = document.getElementById("TextBox1").value;
          var DOB = document.getElementById("TextBox2").value;
          var Add = document.getElementById("TextBox3").value;
          var phn = document.getElementById("TextBox4").value;
          var Country = document.getElementById("DropDownList1").value;
          var State = document.getElementById("DropDownList2").value;
          var City = document.getElementById("DropDownList3").value;
          var msg = "Name: " + Name + ",DOB: " + DOB + ",Add: " + Add + ", phno: " + phn + ",Country: " + Country + ",State: " + State + ",City: " + City;
          return confirm(msg);
      }



请纠正我。但我想只在数据库中发送值而不是名称以避免冗余。谢谢。在我的下拉列表中,文本是可见的而不是值。


please correct me. But i want to send values only in database not names to avoid redundancy.Thank u. In my dropdownlist text is visible not value.

推荐答案

下面是相同的语法。



Below is the syntax for the same.

var ddlCountry = document.getElementById("DropDownList1")
var Country =ddlCountry.options[ddlCountry.selectedIndex].text;


参考 - Asp.net获取下拉列表中的选定值和文本 [ ^ ]。

Refer - Asp.net Get Dropdown Selected value and text in JavaScript[^].
<script type="text/javascript">
function GetCountryDetails() {
    // Get id of dropdownlist
    var parm = document.getElementById("ddlCountry");

    // Get Dropdownlist selected item text
    document.getElementById('lbltxt').innerHTML = parm.options[parm.selectedIndex].text;

    // Get Dropdownlist selected value item
    document.getElementById('lblid').innerHTML = parm.options[parm.selectedIndex].value;
}
</script>



您还可以在那里看到演示。


You can also see the demo there.


这篇关于使用javascript提交表单时如何获取“文本”而不​​是“值”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆