简体   繁体   English

通过单击按钮更改输入值

[英]change input value by clicking button

I am trying to change the value of input by clicking button.我试图通过单击按钮来更改输入的值。 When i click the button the value of input is changed but it hide.当我单击按钮时,输入的值发生了变化,但它隐藏了。 My HTML code is -我的 HTML 代码是 -

<div id="Tabs">
<ul>
<li id="li_tab1" onclick="tab('tab1')" ><a>Lounge/Family Room</a></li>
</ul>
<div id="tab1">
<p>This is the text for tab 1</p>
<input type="button" value="-" />
<input type="number"id="inputtag" style="width:5%;" value="0"/>
<input type="button" value="+" onclick="tab()"/>
</div>

And SCRIPT is-而脚本是——

<script type="text/javascript">
function tab(tab) {
document.getElementById('tab1').style.display = 'none';
document.getElementById('li_tab1').setAttribute("class", "");
document.getElementById(tab).style.display = 'block';
document.getElementById("inputtag").value = "1";
}

someone tell me why this happen...有人告诉我为什么会发生这种情况...

Hope this is what you are expecting希望这是你所期待的

//HTML
<div id="Tabs">
<ul>
    <li id="li_tab1" onclick="tab('tab1')" ><a>Lounge/Family Room</a></li>
</ul>

<div id="tab1" style="display:none">
    <p>This is the text for tab 1</p>
    <input type="button" value="-" />
    <input type="number"id="inputtag" style="width:5%;" value="0"/>
    <input type="button" value="+" onclick="tab()"/>
</div>
</div>


//Script
function tab(tab) {
if(tab)
    document.getElementById(tab).style.display = 'block';
else
  {
    document.getElementById('li_tab1').setAttribute("class", "");
    document.getElementById("inputtag").value = (+document.getElementById("inputtag").value) + 1;
  }
}

Js fiddle Try it Js小提琴试试吧

tab() function first line "document.getElementById('tab1').style.display = 'none';" tab() 函数第一行“document.getElementById('tab1').style.display = 'none';” it means hide your tab1 div and your textbox inside the this div so how can display your textbox ?这意味着将你的 tab1 div 和你的文本框隐藏在这个 div 中,所以如何显示你的文本框? if you are display this textbox then remove your first line in tab() function .如果您要显示此文本框,则删除 tab() 函数中的第一行。

<script type="text/javascript">
function tab(tab) {
document.getElementById('li_tab1').setAttribute("class", "");
document.getElementById(tab).style.display = 'block';
document.getElementById("inputtag").value = "1";
}
</script>

You called the function in two ways.您通过两种方式调用了该函数。 tab('tab1') and tab() tab('tab1') 和 tab()

You want to show the tab after calling tab('tab1'), and want to change the input value after calling tab().您想在调用 tab('tab1') 后显示选项卡,并希望在调用 tab() 后更改输入值。

In order to use the same function to achieve that, you have to use if to write two cases.为了使用相同的函数来实现这一点,您必须使用 if 来编写两种情况。

if (!tab) checks if you are calling tab() whereas the else case checks if you are calling tab('tab1'). if (!tab) 检查您是否正在调用 tab() 而 else case 检查您是否正在调用 tab('tab1')。

 function tab(tab) { if (!tab) { document.getElementById("inputtag").value++; } else { document.getElementById(tab).style.display = 'none'; document.getElementById('li_tab1').setAttribute("class", ""); document.getElementById(tab).style.display = 'block'; } }
 #tab1 { display: none; }
 <div id="Tabs"> <ul> <li id="li_tab1" onclick="tab('tab1')" ><a>Lounge/Family Room</a></li> </ul> <div id="tab1"> <p>This is the text for tab 1</p> <input type="button" value="-" /> <input type="number"id="inputtag" style="width:5%;" value='0'/> <input type="button" value="+" onclick="tab()"/> </div> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM