[英]How to Pass the Text field value to a variable in Javascript
i have the below script where i want to pass the text field value to a variable and display in alert window 我有以下脚本,我要将文本字段值传递给变量并显示在警报窗口中
<form name='searchdata' id="search_form" >
<div class="col-xs-4" id="form_submit_ins">
<label for="usr">Search Instance</label>
<input type="text" class="form-control" id="ins" id"searchval" placeholder="Instance Name"><br/>
<button class="btn btn-danger navbar-btn" onclick="form_submit();" >Search</button>
</div>
<div>
</div>
</form>
</div>
<script type="text/javascript">
function form_submit() {
var item = document.getElementById('searchval').value;
alert(item);
}
You have a typo: 您有错字:
id"searchval"
Should be: 应该:
id="searchval"
id应等于searchval,即id="searchval"
There is something wrong with your HTML (does it have two ID fields? Is one missing a "=" sign? 您的HTML出现问题(它有两个ID字段吗?其中一个缺少“ =”符号吗?
Your code should work if you fix the HTML to: 如果将HTML修复为:
<input type="text" class="form-control" id="searchval" placeholder="Instance Name">
Your input
has two id
's, it can only have one id
您的
input
有两个id
,只能有一个id
change: 更改:
<input type="text" class="form-control" id="ins" id"searchval" placeholder="Instance Name">
To: 至:
<input type="text" class="form-control" id="searchval" placeholder="Instance Name">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.