文本框不能重置 [英] Text box not getting reset
本文介绍了文本框不能重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本框,我试着按下按钮后重置它。但它没有得到重置。
HTML脚本:
< input type =textname =class =input-default id =inp>
< button type =submitclass =btn btn-success btn-sm m-b-10 m-l-5onclick =add()>添加< / button>
< select id ='pre-selected-options'multiple ='multiple'>
< option value ='elem_1'selected> 1< / option>
< option value ='elem_2'> a< / option>
< option value ='elem_3'> x< / option>
<选项值='elem_4'选择> Ce< /选项>
< option value ='elem_100'> Td< / option>
< / select>
< script src =https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js>< / script>
< script src =js / jquery.multi-select.js>< / script>
JavaScript代码: -
var x;
var选项;
var y;
function add(){
var x = document.getElementById('pre-selected-options');
console.log(x);
var option = document.createElement(option);
option.text = document.getElementById(inp).value;
var y = option.text
x.add(option);
$('#pre-selected-options')。multiSelect('addOption',{
value:y,
text:y,
index:0,
嵌套:'optgroup_label'
});
document.getElementById('pre-selected-options')。reset();
}
$('#pre-selected-options')。multiSelect();
我试着添加这个,而不是重置表单:
document.getElementById('pre-selected-options')。reset();
解决方案
您在这里遇到的麻烦是您正在尝试在调用 pre-selected-options
时清除输入框,其中id inp
,但未连接到文本box。
使用 document.getElementById('inp')。value =
应该清除您的值文本框。
I have a text box and I tried to reset it after a button click. But it is not getting reset. HTML Script:-
<input type="text" name="" class="input-default" id="inp">
<button type="submit" class="btn btn-success btn-sm m-b-10 m-l-5" onclick="add()">Add</button>
<select id='pre-selected-options' multiple='multiple'>
<option value='elem_1' selected>1</option>
<option value='elem_2'>a</option>
<option value='elem_3'>x</option>
<option value='elem_4' selected>Ce</option>
<option value='elem_100'>Td</option>
</select>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="js/jquery.multi-select.js"></script>
JavaScript Code :-
var x;
var option;
var y;
function add() {
var x = document.getElementById('pre-selected-options');
console.log(x);
var option = document.createElement("option");
option.text = document.getElementById("inp").value;
var y = option.text
x.add(option);
$('#pre-selected-options').multiSelect('addOption', {
value: y,
text: y,
index: 0,
nested: 'optgroup_label'
});
document.getElementById('pre-selected-options').reset();
}
$('#pre-selected-options').multiSelect();
I tried adding this and its not resetting the form :
document.getElementById('pre-selected-options').reset();
解决方案
The trouble you are facing here is that you are trying to clear the input box with id inp
while calling pre-selected-options
, which isn't connected to your text box.
Using document.getElementById('inp').value = ""
should clear the value of your textbox.
这篇关于文本框不能重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文