简体   繁体   English

谁能帮我告诉我为什么我在函数中得到一个空值

[英]Can anyone help and tell me why do i get a null value in the function

Heres the Code这是代码

This is the Script这是脚本

 <script>
 $(document).ready(function(){    
   $('#done_edit').click(function(){

    var name= document.getElementById('first_name').value;
    alert(name);
   });
});

This is for the form这是为了表格

<form id="passedit" class="submit">
<label>First Name:</label><input type="text"  name="first_name" class="form-control" value="<?php echo $row['name'];?>" />

<button type="submit" class="btn btn-info" id="done_edit" >DONE<span class="glyphicon glyphicon-check"></span></button>

Because you don't have an element with an Id set to first_name因为您没有将 Id 设置为 first_name 的元素

you should have something like你应该有类似的东西

<input type="text" id="first_name" class="form-control">

var name= document.getElementById('first_name'); var name= document.getElementById('first_name'); You should have an element wich has the ID as "firstname".您应该有一个 ID 为“名字”的元素。 For example,`例如,`

<input type="text" id="first_name" value="Type your text here"/> Then you'll get the output as an object. <input type="text" id="first_name" value="Type your text here"/>然后你会得到一个对象的输出。

If you rewrite the mentioned line as follows,如果您按如下方式重写上述行,

var name= document.getElementById('first_name').value; You'll get "Type your text here" in your alert box.您将在警报框中看到“在此处输入您的文字”。 ie: Change the attribute name to id in your textbox.即:在文本框中将属性名称更改为 id。

Now you have in the alert the value of the element.现在您在警报中有元素的值。

HTML HTML

<form id="passedit" class="submit">
<label>ID Number:</label><input id="first_name" type="text" 
 name="id_number" class="form-control" value="<?php echo 
 $row['name'];?>" />
<button type=

JS JS

 $(document).ready(function(){    
   $('#done_edit').click(function(){
      var name= document.getElementById('first_name').value;
      alert(name);
   });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁能告诉我为什么在尝试调用此函数时会出错? - Can anyone tell me why I get a error when i try to call this function? 谁能告诉我为什么不返回值? - Can anyone tell me why the value is not returned? 当我将 animation 效果包装在 function 中时,它停止工作。 谁能告诉我为什么? - When I wrap my animation effect in a function, it stops working. Can anyone tell me why? 谁能告诉我为什么这一次有效? - Can anyone tell me why this is working once? 谁能帮我解释一下“ get:function()”和.prototype? - Can anyone help explain “get: function()” and .prototype to me? 任何人都可以帮助我如何使用JavaScript获取价值? - Can anyone help me how to get value using javascript? 减少 function 执行 - 谁能帮我识别错误,因为我无法得到结果 - Reduce function execution - Can anyone help me in identifying the error as I am unable get the result 谁能告诉我为什么我收到错误'无法读取未定义的属性'值''? P5.JS - Can anyone tell me why i am getting the error 'Cannot read property 'value' of undefined'? P5.JS 谁能帮我破译此功能? - Can anyone help me decipher this function? 谁能帮我弄清楚为什么我无法让jQuery震动效果在我的div上起作用? - Can anyone help me in figuring out why I can't get Jquery shake effect to work on my div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM