javascript - 隐藏文本框,直到选中相关复选框 - Javascript

标签 javascript jquery html css

我能有一些 js 的建议吗。

我希望在选中相关复选框之前隐藏数量 (qty) 文本框。

类似 if else 函数的东西会很棒,我不希望表单的位置移动,而只是让 qty 文本框出现。

这是一个非评分元素,我还在学习,我只学习了 8 周,每周 2.5 小时,js 不是我的强项。如果你能得到,我绝对不介意运行总计,将价格乘以隐藏的数量,直到添加第一个数量以及选择的选项。

谢谢....

http://codepen.io/anon/pen/PwRoGd

HTML:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<title>Test Practice V3 - Order</title>
<link rel="stylesheet" href="stylehomepage.css">
<script src="script.js"></script>
</head>
<body>
<div id="ocontainer">
    <div id="buttons">
        <div id="homebutton">
          <a href="index.html">home</a>
        </div>
        <div id="menubutton">
          <a href="menu.html">menu</a>
        </div>
        <div id="orderbutton">
          <a href="order.html">order</a>
        </div>
        <div id="fbackbutton">
        <a href="feedback.html">feedback</a>    
        </div>
    </div>

<div id="content"> 
    <br>
    <br>
    <form>
        <legend><b>Chips:</b></legend>
        <select  name="chips" onchange="calculateTotal()">
            <option value="None">Select Size</option>
            <option value="XS">X-Small ($2)</option>
            <option value="S">Small ($3)</option>
            <option value="M">Medium ($4)</option>
            <option value="L">Large ($5)</option>
            <option value="XL">X-Large ($6)</option>
        </select>
    </form>  
    <br>
    <br>
   <table>
   <tr class="cboxtable">
    <th> </th>
    <th><b>Snack:</b></th>      
    <th><b>Price:</b></th>
    <th><b>Quantity:</b></th>
  </tr>
  <tr>
    <td class="cboxtable">
    <form>
        <input type="checkbox" name="calamari">
    </form></td>
    <td>Calamari (10 Pieces)</td>
    <td>$6.50</td>      
    <td>
    <form>
        <input type="text" size="10" name="calamariqty">
    </form>
    </td>
  </tr>
</table>
<br>
<br>
<form>
    <input type="submit" value="Send">
    <input type="reset" value="Reset">
</form>



    </div>


    </div>
</div>
</body>
</html>

CSS:

body {
    background-image:url(Images/Water%20Image.jpg);
    background-attachment:fixed;
    background-repeat:no-repeat;
    font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
#container {
    width: 80%;
    height: 800px;
    background-color: #FFF;
    margin-left: auto;
    margin-right: auto;
    margin-top: 100px;
    margin-bottom: 20px;
    box-shadow: 10px 10px 20px #333;
    border-radius: 30px;
    padding:20px;
}
#ocontainer {
    width: 80%;
    height: 100%;
    background-color: #FFF;
    margin-left: auto;
    margin-right: auto;
    margin-top: 100px;
    margin-bottom: 20px;
    box-shadow: 10px 10px 20px #333;
    border-radius: 30px;
    padding:20px;
}
#buttons {
    height:50px;
    width:85%;
    background-color:#FFF;
    margin-left:auto;
    margin-right:auto;
}
#homebutton {
    height: 100%;
    width: 23.5%;
    float: left;
    background-color: #FFF;
    border-radius: 15px;
    margin-right:2%;
}
#menubutton {
    height:100%;
    width:23.5%;
    float:left;
    background-color:#FFF;
    border-radius: 15px;
    margin-right:2%;
}
#orderbutton {
    height:100%;
    width:23.5%;
    float:left;
    background-color:#FFF;
    border-radius: 15px;
    margin-right:2%;
}
#fbackbutton {
    height:100%;
    width:23.5%;
    float:left;
    background-color:#FFF;
    border-radius: 15px;
}
a {
    display:block;
    text-decoration: none;
    text-transform: uppercase;
    color:black;
    font-size:18px;
    height:50px;
    width:100%;
    margin-right:0;
    margin-left:0px;
    text-align:center;
    line-height:50px;   
}
#homebutton:hover {
    background-color:#00BBFF;
}
#menubutton:hover {
    background-color:#00BBFF;
}
#orderbutton:hover {
    background-color:#00BBFF;
}
#fbackbutton:hover {
    background-color:#00BBFF;
}
#content {
    width: 95%;
    height: 80%;
    padding-top:30px;
    padding: 10px;
    margin-left:auto;
    margin-right:auto;
    font-size:16px;
}
table {
    width:65%;
}
tr {
    text-align:left;
}
cboxtable {
    max-width:50px;
}
th {
    font-size:18px;
}
legend {
    font-size:18px;
}
option {
    font-size:16px;
}

最佳答案

您需要将更改事件附加到复选框。其中,使用选中元素的 checked 属性来切换文本元素可见性:

$(':checkbox[name=calamari]').change(function(){
   $(':text[name=calamariqty]').toggle(this.checked)
});

Demo

关于javascript - 隐藏文本框,直到选中相关复选框 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28561192/

相关文章:

javascript - 在进行 JQuery 对话框选择之前,HTML 表单继续提交?

javascript - Bootstrap 菜单切换不适用于多个切换链接

html - 如何在 Express (Node.js) 模板中为 Internet Explorer 保留条件 CSS 语句

css - 带滚动的垂直 div 填充所有可用的垂直空间

javascript - 使用一个巨大的状态对象

javascript - Vue-cli webpack模板中main.js如何链接到Index.html

javascript - 如何获取 ID 作为值和名称以显示在 dropdownlist php 中

javascript - Bootstrap 模态数据关闭不起作用

php - 如何为链接管理器创建自定义菜单以显示在 Wordpress 的导航下

javascript - jQuery 修改 DOM 内元素的内联 CSS