如何使用Javascript从HTML中获取元素数据?

时间:2016-12-01 11:01:08

标签: javascript html

HTML代码

<body>
       <table>
                    <tr>
                        <td><label style="text-align:left">Cari Kod:</label></td>
                        <td><input type="text" id="cariKod" value="" /> </td>
                    </tr>
                    <tr>
                        <td> <label style="text-align:left">Cari İsim:</label></td>
                        <td><textarea rows="4" id="cariAd" cols="50" name="comment" form="usrform"></textarea></td>
                    </tr>
        </table>
    </body>

我想用Javascript

从html元素中获取数据

Javascript代码

<script>

        var a =$("cariKod");

        $(document).ready(function () {

            $("#btn1").click(function () {
                alert($("#a"))
            });

        });
        </script>

7 个答案:

答案 0 :(得分:0)

您可以尝试以下

<script>


     $(document).ready(function () {
        var a =$("#cariKod");   //$("cariKod");
         $("#btn1").click(function () {
                alert(a.val());      //alert($("#a"))
         });
      });
</script>

答案 1 :(得分:0)

检查以下代码: 只需在警报

时追加.val()
<script>

        var a =$("#cariKod");

        $(document).ready(function () {

            $("#btn1").click(function () {
                alert(a.val()); 
            });

        });
        </script>

答案 2 :(得分:0)

首先,您没有正确选择输入:$("#cariKod")而不是$("cariKod")。 其次,使用.val()

访问该值

有关工作示例,请参阅此代码段。

$(document).ready(function () {

var a =$("#cariKod");

$("#btn1").click(function () {
    alert(a.val())
});

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
       <table>
                    <tr>
                        <td><label style="text-align:left">Cari Kod:</label></td>
                        <td><input type="text" id="cariKod" value="" /> </td>
                    </tr>
                    <tr>
                        <td> <label style="text-align:left">Cari İsim:</label></td>
                        <td><textarea rows="4" id="cariAd" cols="50" name="comment" form="usrform"></textarea></td>
                    </tr>
        </table>
    </body>

<button id="btn1">Button</button>

答案 3 :(得分:0)

复制粘贴此代码并尝试使用您的broswer,而不是使用代码段

$(function(){
	
var $value1 = $('#cariKod').val();
var $value2 = $('#cariAd').val(); 

$('#button').on('click', function(){
	alert('first value is ' + $value1 + '  , second value is ' + $value2);
});	

});
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<table>
  <tr>
    <td><label style="text-align:left">Cari Kod:</label></td>
    <td><input type="text" id="cariKod" value="" /></td>
  </tr>
  <tr>
    <td><label style="text-align:left">Cari İsim:</label></td>
    <td><textarea rows="4" id="cariAd" cols="50" name="comment" form="usrform"></textarea></td>
  </tr>
</table>

<button id="button">Click me</button>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

</body>
</html>

答案 4 :(得分:0)

Parameter is not valid.
at System.Drawing.Graphics.GetHdc()
   at Telerik.Reporting.Pdf.PdfContext..ctor()
   at Telerik.Reporting.Pdf.PdfDocument..ctor()
   at Telerik.Reporting.ImageRendering.DocumentPdf.FindOrCreateDocument()
   at Telerik.Reporting.ImageRendering.DocumentPdf..ctor(PdfRenderingContext context, IMeasureContext measureContext)
   at Telerik.Reporting.ImageRendering.PdfReport.CreateDocument(IDictionary renderingInfo, IDictionary deviceInfo, CreateStream createStreamCallback, PageSettings pageSettings)
   at Telerik.Reporting.BaseRendering.DocumentRenderingExtensionBase.CreateWriter(IDictionary renderingContext, IDictionary deviceInfo, CreateStream createStreamCallback, PageSettings pageSettings)
   at Telerik.Reporting.ImageRendering.PdfReport.CreateWriter(IDictionary renderingContext, IDictionary deviceInfo, CreateStream createStreamCallback, PageSettings pageSettings)
   at Telerik.Reporting.BaseRendering.RenderingExtensionBase.Render(Report report, Hashtable renderingContext, Hashtable deviceInfo, CreateStream createStreamCallback, EvaluateHeaderFooterExpressions evalHeaderFooterCallback)
   at Telerik.Reporting.Processing.ReportProcessor.RenderCore(ExtensionInfo extensionInfo, IList`1 reports, Hashtable deviceInfo, IRenderingContext renderingContext, CreateStream createStreamCallback)
   at Telerik.Reporting.Processing.ReportProcessor.ProcessAndRender(String format, ReportSource reportSource, Hashtable deviceInfo, IRenderingContext renderingContext, CreateStream createStreamCallback)
   at Telerik.Reporting.Processing.ReportProcessor.ProcessAndRenderStateless(String format, ReportSource reportSource, Hashtable deviceInfo, IRenderingContext renderingContext, CreateStream createStreamCallback)
   at Telerik.Reporting.Processing.ReportProcessor.RenderReport(String format, ReportSource reportSource, Hashtable deviceInfo)

答案 5 :(得分:0)

这是解决方案。

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
var a;

    $(document).ready(function () {

        $("#btn1").click(function () {
            a = $('#cariKod').val();
            alert(a);
        });
    });

HTML

<html>
   <body>
       <table>
<tr>
    <td><label style="text-align:left">Cari Kod:</label></td>
    <td><input type="text" id="cariKod" value="" /> </td>
</tr>
<tr>
    <td> <label style="text-align:left">Cari İsim:</label></td>
    <td><textarea rows="4" id="cariAd" cols="50" name="comment" form="usrform"></textarea></td>
</tr>
<tr><td><button type="button" id="btn1">Submit</button></td></tr>

   </body>

</html>

答案 6 :(得分:0)

    $(document).ready(function () {
        $("#btn1").click(function () {

        var value = $("#cariKod").val();
         alert(value);

        });

    });

它有效