隐藏输入按钮

时间:2012-10-24 14:37:12

标签: javascript jquery html

以下是我的代码:http://jsfiddle.net/Draven/rEPXM/23/

我想知道如何隐藏添加提交按钮,直到我点击 + 图像将输入框添加到表单中。

我不想在输入框旁边添加提交按钮,因为我希望能够添加多个输入框并在单击添加时全部提交。

HTML

<div id="left">      
  <div class="box">
    <div class="boxtitle"><span class="boxtitleleftgap">&nbsp;</span><span class="boxtitlebulk"><span class="boxtitletext">Folders</span><div style="float: right; margin-top: 4px;"><a href="#" onclick="javascript: AddFolder();"><div class="addplus">&nbsp;</div></a></div></span></div>
      <div class="boxcontent">
        <form method="post" id="folderform" action="page.php?action=list-volunteer-apps" name="folderform">
          <a class="even" href="page.php?action=list-volunteer-apps&folder=2">Folder 2 <span class="text">(1)</span></a><a class="even" href="page.php?action=list-volunteer-apps&folder=1">Folder 1 <span class="text">(0)</span></a>
          <div id="foldercontainer"><input type="submit" value="Add"></div>
        </form>
      </div>
  </div>
</div>

的jQuery

function AddFolder() {
    $('#foldercontainer').append('<input name="folder[]" type="text" size="20" />');
}​

4 个答案:

答案 0 :(得分:17)

只需给按钮一个ID,然后让它开始隐藏

<input type="submit" id="addButton" value="Add" style="display: none;">

然后使用show() jQuery方法:

$("#addButton").show();

http://jsfiddle.net/TcFhy/

答案 1 :(得分:2)

这是一种可以做到这一点的方法......同时,清理了用于制作这些输入框的方法:

http://jsfiddle.net/mori57/4JANS/

所以,在你的html中你可能有:

  <div id="foldercontainer">
      <input id="addSubmit" type="submit" value="Add">
      <input id="folderName" name="folder[]" type="text" size="20" style="" />
  </div>

你的CSS可能是:

#foldercontainer #addSubmit {
    display:none;
}
#foldercontainer #folderName {
    display:none;
    width: 120px; 
    background: #FFF url(http://oi47.tinypic.com/2r2lqp2.jpg) repeat-x top left; 
    color: #000; 
    border: 1px solid #cdc2ab; 
    padding: 2px; 
    margin: 0px; 
    vertical-align: middle;
}

你的脚本可能是:

// set up a variable to test if the add area is visible
// and another to keep count of the add-folder text boxes
var is_vis = false,
    folderAddCt = 0;

function AddFolder() {
    if(is_vis == false){
        // if it's not visible, show the input boxes and 
        $('#foldercontainer input').show();
        // set the flag true
        is_vis = true;
    } else {
        // if visible, create a clone of the first add-folder
        // text box with the .clone() method
        $folderTB = $("#folderName").clone();
        // give it a unique ID
        $folderTB.attr("id","folderName_" + folderAddCt++);
        // and append it to the container
        $("#foldercontainer").append($folderTB);
    }
}​

答案 2 :(得分:1)

我将按钮从文件夹换行中移出,我在添加新文件夹时显示它。这样,添加新文件夹时按钮将保持在底部。我还删除了内联样式,并将其替换为类。

这用于显示按钮,只需将其添加到AddFolder()功能:

$('#addBtn').show();

我正在用CSS隐藏它:

#addBtn { display: none;}

我将按钮移出#foldercontainer,这样当您添加多个文件夹时,它会始终保持在底部:

<div id="foldercontainer"></div>
<input id="addBtn" type="submit" value="Add">

在这里查看jsFiddle:http://jsfiddle.net/kmx4Y/1/

答案 3 :(得分:0)

$('form#folderform input[type=submit]').hide();

然后在您单击提交

后显示添加按钮

http://jsfiddle.net/SQh8L/