2017-03-31 51 views
0

我在用戶提交之前使用Bootstrap模式創建表單確認窗口。在模態窗口引導程序/ jQuery上顯示錶單條目

這裏是HTML的一部分:

<form role="form"> 
    <div class="form-group"> 
     <label for="Company" class="col-sm-3 control-label">Company:</label> 
     <div class="col-sm-9"> 
     <input type="text" name="Company" id="Company" size="50" maxlength="100" class="form-control" placeholder="Companyt"> 
     </div> 
    </div> 

    <div class="form-group"> 
     <label for="Department" class="col-sm-3 control-label">Department:</label> 
     <div class="col-sm-9"> 
     <input type="text" name="Department" id="Department" size="50" maxlength="100" class="form-control" placeholder="Department"> 
     </div> 
    </div> 

    <div class="form-group"> 
     <label for="Username" class="col-sm-3 control-label">Name:</label> 
     <div class="col-sm-9"> 
     <input type="text" name="Username" id="Username" size="50" maxlength="100" class="form-control" placeholder="Username"> 
     </div> 
    </div> 

    <div class="form-group"> 
     <label for="Address" class="col-sm-3 control-label">Address:</label> 
     <div class="col-sm-9"> 
     <input type="text" name="Address" id="Address" size="50" maxlength="100" class="form-control" placeholder="Address"> 
     </div> 
    </div> 

    <div class="form-group"> 
     <label for="TEL" class="col-sm-3 control-label">TEL:</label> 
     <div class="col-sm-9"> 
     <input type="text" name="TEL" id="TEL" size="50" maxlength="100" class="form-control" placeholder="TEL"> 
     </div> 
    </div> 

    <div class="form-group"> 
     <label for="Email" class="col-sm-3 control-label">Email:</label> 
     <div class="col-sm-9"> 
     <input type="text" name="Email" id="Email" size="50" maxlength="100" class="form-control" placeholder="Email"> 
     </div> 
    </div> 
    <input type="button" class="btn btn-primary btn-block enduserinfobtn" id="submitBtn" data-toggle="modal" data-target="#myModal" value="Submit"> 
    </form> 
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
    <div class="modal-dialog" role="document"> 
     <div class="modal-content"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
      <h4 class="modal-title" id="myModalLabel">Confirm your entry</h4> 
     </div> 
     <div class="modal-body"> 
      <div class="row"> 
      <div class="entryname col-xs-3 ">Company:</div> 
      <div class="entrydata col-xs-9" id="mCompany"></div> 
      </div> 
      <div class="row"> 
      <div class="entryname col-xs-3">Department:</div> 
      <div class="entrydata col-xs-9" id="mDepartment"></div> 
      </div> 
      <div class="row"> 
      <div class="entryname col-xs-3">Name:</div> 
      <div class="entrydata col-xs-9" id="mUsername"></div> 
      </div> 
      <div class="row"> 
      <div class="entryname col-xs-3">Address:</div> 
      <div class="entrydata col-xs-9" id="mAddress"></div> 
      </div> 
      <div class="row"> 
      <div class="entryname col-xs-3">TEL:</div> 
      <div class="entrydata col-xs-9" id="mTel"></div> 
      </div> 
      <div class="row"> 
      <div class="entryname col-xs-3">Email:</div> 
      <div class="entrydata col-xs-9" id="mEmail"></div> 
      </div> 
      <div class="clearfix"></div> 
     </div> 
     <div class="modal-footer"> 
      <div class="col-xs-6"><button class="btn btn-block btn-default" data-dismiss="modal">Cancel</button></div> 
      <div class="col-xs-6"><button class="btn btn-block btn-primary">Submit</button></div> 
     </div> 
     </div> 
    </div> 
    </div> 

這裏是jQuery的部分:

$('#submitBtn').click(function() { 
    $('#mCompany').text($('#Company').val()); 
    $('#mDepartment').text($('#Department').val()); 
    $('#mUsername').text($('#Username').val()); 
    $('#mAddress').text($('#Address').val()); 
    $('#mTEL').text($('#TEL').val()); 
    $('#mEmail').text($('#Email').val()); 
}); 

您可以看到與此 link to JSFiddle.

需要不是所有的領域嘗試,而我想要做的只是顯示帶有數據的條目。

例如在下圖中,我不想讓紅色邊框部分顯示出來。

Example Screen Shot

上的jsfiddle樣品只有6場,但我的形式有超過30場。我想知道是否有一種方法只顯示數據字段。

+0

等待,你想不顯示ADRESS和電話領域?你可以從你的submitBtn中刪除這些字段,或者我在這裏丟失了什麼? 編輯:沒關係,我現在明白了。 –

回答

0

以下部分從HTML

<div class="row"> 
<div class="entryname col-xs-3">Address:</div> 
<div class="entrydata col-xs-9" id="mAddress"></div> 
</div> 
<div class="row"> 
<div class="entryname col-xs-3">TEL:</div> 
<div class="entrydata col-xs-9" id="mTel"></div> 
</div> 

卸下jQuery的

$('#mAddress').text($('#Address').val()); 
$('#mTEL').text($('#TEL').val()); 
0

刪除下面的部分我做一些improvents:


 
    var all = ""; 
 
    $('#submitBtn').click(function() { 
 
    /* when the button in the form, 
 
    display the entered values in the modal */ 
 
    //console.log($("from input")); 
 
    all = ""; 
 
    $.each($("form input"),function(i,v){ 
 
    var id= $(this).attr('id'); 
 
    \t //console.log(id); 
 
     //console.log("#m"+id); 
 
    \t $("#m"+id).text($(this).val()); 
 
     all = all + $(this).val(); 
 
    }); 
 
    hideEmptyInputs();  
 
    }); 
 
    $('#myModal').on('show.bs.modal', function (e) { 
 
      if(all == ""){ 
 
      \t e.preventDefault(); 
 
      } 
 
    }) 
 
    
 
    function hideEmptyInputs(){ 
 
    \t //console.log($("#myModal div")); 
 
    \t //console.log($("#myModal div[id^=m]")); 
 
    $.each($("#myModal div[id^=m]"),function(i,v){ 
 
    \t //console.log($(this).text()); 
 
     var value = $(this).text(); 
 
     if(value == ""){ 
 
     \t //Hide if empty 
 
     $(this).parent().css('display','none'); 
 
     }else{ 
 
     $(this).parent().css('display','block');  
 
     } 
 
    }); 
 
    \t //console.log("algo"); 
 
    }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<form role="form"> 
 
     <div class="form-group"> 
 
      <label for="Company" class="col-sm-3 control-label">Company:</label> 
 
      <div class="col-sm-9"> 
 
      <input type="text" name="Company" id="Company" size="50" maxlength="100" class="form-control" placeholder="Companyt"> 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      <label for="Department" class="col-sm-3 control-label">Department:</label> 
 
      <div class="col-sm-9"> 
 
      <input type="text" name="Department" id="Department" size="50" maxlength="100" class="form-control" placeholder="Department"> 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      <label for="Username" class="col-sm-3 control-label">Name:</label> 
 
      <div class="col-sm-9"> 
 
      <input type="text" name="Username" id="Username" size="50" maxlength="100" class="form-control" placeholder="Username"> 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      <label for="Address" class="col-sm-3 control-label">Address:</label> 
 
      <div class="col-sm-9"> 
 
      <input type="text" name="Address" id="Address" size="50" maxlength="100" class="form-control" placeholder="Address"> 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      <label for="TEL" class="col-sm-3 control-label">TEL:</label> 
 
      <div class="col-sm-9"> 
 
      <input type="text" name="TEL" id="TEL" size="50" maxlength="100" class="form-control" placeholder="TEL"> 
 
      </div> 
 
     </div> 
 
     <button type="button" class="btn btn-primary btn-block enduserinfobtn" id="submitBtn" data-toggle="modal" data-target="#myModal" value="Submit">Submit</button> 
 
</form> 
 
<!-- ########## Start Modal Section ########## --> 
 
     <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
 
     <div class="modal-dialog" role="document"> 
 
      <div class="modal-content"> 
 
      <div class="modal-header"> 
 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
 
       <h4 class="modal-title" id="myModalLabel">Confirm your entry</h4> 
 
      </div> 
 
      <div class="modal-body"> 
 
       <div class="row"> 
 
       <div class="entryname col-xs-3 ">Company:</div> 
 
       <div class="entrydata col-xs-9" id="mCompany"></div> 
 
       </div> 
 
       <div class="row"> 
 
       <div class="entryname col-xs-3">Department:</div> 
 
       <div class="entrydata col-xs-9" id="mDepartment"></div> 
 
       </div> 
 
       <div class="row"> 
 
       <div class="entryname col-xs-3">Name:</div> 
 
       <div class="entrydata col-xs-9" id="mUsername"></div> 
 
       </div> 
 
       <div class="row"> 
 
       <div class="entryname col-xs-3">Address:</div> 
 
       <div class="entrydata col-xs-9" id="mAddress"></div> 
 
       </div> 
 
       <div class="row"> 
 
       <div class="entryname col-xs-3">TEL:</div> 
 
       <div class="entrydata col-xs-9" id="mTEL"></div> 
 
       </div> 
 
       <div class="row"> 
 
       <div class="entryname col-xs-3">Email:</div> 
 
       <div class="entrydata col-xs-9" id="mEmail"></div> 
 
       </div> 
 
       <div class="clearfix"></div> 
 
      </div> 
 
      <div class="modal-footer"> 
 
       <div class="col-xs-6"><button class="btn btn-block btn-default" data-dismiss="modal">Cancel</button></div> 
 
       <div class="col-xs-6"><button class="btn btn-block btn-primary">Submit</button></div> 
 
      </div> 
 
      </div> 
 
     </div> 
 
     </div> 
 
     <!-- ########## End Modal Section ########## -->

您可以添加一些類就知道你要什麼領域的模式顯示什麼不顯示....我想可能是動態增加了模式太...

添加輸入動態:


 
    var all = ""; 
 
    $('#submitBtn').click(function() { 
 
    /* when the button in the form, 
 
    display the entered values in the modal */ 
 
    //console.log($("from input")); 
 
    all = ""; 
 
    
 
     $("#modalBody").html(''); 
 
    $.each($("form input"),function(i,v){ 
 
    var id= $(this).attr('id'); 
 
    if($(this).hasClass('add')){ 
 
     all = all + $(this).val(); 
 
     var input = '<div class="row">' 
 
     + '<div class="entryname col-xs-3">' + id + ':</div>' 
 
     + '<div class="entrydata col-xs-9" id="m' + id +'">' + $(this).val(); + '</div>' 
 
     + '  </div>'; 
 
     
 
     //console.log(input); 
 
     
 
     $("#modalBody").append(input); 
 
     
 
    } 
 
    \t //console.log(id); 
 
     //console.log("#m"+id); 
 
    \t //$("#m"+id).text($(this).val()); 
 
    // all = all + $(this).val(); 
 
    }); 
 
    hideEmptyInputs();  
 
    }); 
 
    $('#myModal').on('show.bs.modal', function (e) { 
 
      if(all == ""){ 
 
      \t e.preventDefault(); 
 
      } 
 
    }) 
 
    
 
    function hideEmptyInputs(){ 
 
    \t //console.log($("#myModal div")); 
 
    \t //console.log($("#myModal div[id^=m]")); 
 
    $.each($("#myModal div[id^=m]"),function(i,v){ 
 
    \t //console.log($(this).text()); 
 
     var value = $(this).text(); 
 
     if(value == ""){ 
 
     \t //Hide if empty 
 
     $(this).parent().css('display','none'); 
 
     }else{ 
 
     $(this).parent().css('display','block');  
 
     } 
 
    }); 
 
    \t //console.log("algo"); 
 
    }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<form role="form"> 
 
     <div class="form-group"> 
 
      <label for="Company" class="col-sm-3 control-label">Company:</label> 
 
      <div class="col-sm-9"> 
 
      <input type="text" name="Company" id="Company" size="50" maxlength="100" class="form-control add" placeholder="Companyt"> 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      <label for="Department" class="col-sm-3 control-label">Department:</label> 
 
      <div class="col-sm-9"> 
 
      <input type="text" name="Department" id="Department" size="50" maxlength="100" class="form-control add" placeholder="Department"> 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      <label for="Username" class="col-sm-3 control-label">Name:</label> 
 
      <div class="col-sm-9"> 
 
      <input type="text" name="Username" id="Username" size="50" maxlength="100" class="form-control add" placeholder="Username"> 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      <label for="Address" class="col-sm-3 control-label">Address:</label> 
 
      <div class="col-sm-9"> 
 
      <input type="text" name="Address" id="Address" size="50" maxlength="100" class="form-control" placeholder="Address"> 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      <label for="TEL" class="col-sm-3 control-label">TEL:</label> 
 
      <div class="col-sm-9"> 
 
      <input type="text" name="TEL" id="TEL" size="50" maxlength="100" class="form-control add" placeholder="TEL"> 
 
      </div> 
 
     </div> 
 
     <button type="button" class="btn btn-primary btn-block enduserinfobtn" id="submitBtn" data-toggle="modal" data-target="#myModal" value="Submit">Submit</button> 
 
</form> 
 
<!-- ########## Start Modal Section ########## --> 
 
     <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
 
     <div class="modal-dialog" role="document"> 
 
      <div class="modal-content"> 
 
      <div class="modal-header"> 
 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
 
       <h4 class="modal-title" id="myModalLabel">Confirm your entry</h4> 
 
      </div> 
 
      <div class="modal-body"> 
 
      <div id="modalBody"></div> 
 
       <div class="clearfix"></div> 
 
      </div> 
 
      <div class="modal-footer"> 
 
       <div class="col-xs-6"><button class="btn btn-block btn-default" data-dismiss="modal">Cancel</button></div> 
 
       <div class="col-xs-6"><button class="btn btn-block btn-primary">Submit</button></div> 
 
      </div> 
 
      </div> 
 
     </div> 
 
     </div> 
 
     <!-- ########## End Modal Section ########## -->

+1

謝謝你的回答。您發佈的第一個代碼似乎運行良好,但第二個代碼與地址字段存在問題。這兩個有什麼區別? –

+1

我從你的答案中找出答案。謝謝! 我將$(「表單輸入」)更改爲$(「:input」),因此它也適用於其他輸入。 –

+0

我只是添加一個類... class =「add」< - 只顯示這個類的... –