JS和Jquery获取选中select值和文本

淡淡的烟草味﹌ 2022-07-12 03:25 304阅读 0赞
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>JS和Jquery获取选中select值和文本</title>
  6. </head>
  7. <body>
  8. <form name="form1" id="form1" method="post" action="XX">
  9. <input name="gettext" id="gettext" value="" type="hidden" />
  10. <input name="getvalue" id="getvalue" value="" type="hidden" />
  11. <!--<select name="PaymentType" style="width:110px" οnchange="GettextAndValue(this)"> -->
  12. <select name="PaymentType" style="width:110px" οnchange="GettextAndValue2(this)">
  13. <option value="">请选择 </option>
  14. <option value="001">月付</option>
  15. <option value="002">半年付</option>
  16. <option value="003">年付</option>
  17. </select>
  18. </form>
  19. <p><a href="javascript:;" οnclick="subform();">DOM</a></p>
  20. <p><a href="javascript:;" οnclick="subform2();">JQ</a></p>
  21. <script src="http://code.jquery.com/jquery-latest.js"></script>
  22. <script language="javascript">
  23. //DOM原生获取select选中下拉框的的值和文本
  24. function GettextAndValue(obj){
  25. var txt=obj.options[obj.options.selectedIndex].text;
  26. var val=obj.options[obj.options.selectedIndex].value;
  27. document.getElementById("gettext").value = txt;
  28. document.getElementById("getvalue").value = val;
  29. }
  30. //DOM原生表单提交
  31. function subform(){
  32. alert(document.getElementById("gettext").value);
  33. alert(document.getElementById("getvalue").value);
  34. document.getElementById("form1").submit();
  35. }
  36. //JQ获取select选中下拉框的的值和文本
  37. function GettextAndValue2(obj){
  38. var txt=$(obj).find("option:selected").text();
  39. var val=$(obj).find("option:selected").val();
  40. $("#gettext").val(txt);
  41. $("#getvalue").val(val);
  42. }
  43. //JQ表单提交
  44. function subform2(){
  45. alert($("#gettext").val());
  46. alert($("#getvalue").val());
  47. $("#form1").submit();
  48. }
  49. </script>
  50. </body>
  51. </html>

发表评论

表情:
评论列表 (有 0 条评论,304人围观)

还没有评论,来说两句吧...

相关阅读