在JavaScript中应用条件语句进行打印

时间:2019-04-16 10:06:15

标签: javascript jquery html

我有一个耐心的表格。而且我想在保存到数据库之前打印表格。 格式为private boolean loadFragment(Fragment fragment) { if (fragment != null) { getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment_container, fragment) .commit(); } return false; } text,复选框`的字段有几种类型。

现在我只想打印那些radio的{​​{1}}和那些选中的checkbox按钮。

html

checked
radio

但是我没有任何结果,尽管我的var data = '<html><head><style media="print"></style></head><body><div class="row" style="border:2px solid #000;"><div class="col-md-12"><b>Name :</b>' + name + '</div><div class="col-md-6"><b>Gender :</b>' + if (sex == 1) { document.write("Male"); } else if (sex == 2) { document.write("Female"); } else { document.write("Other"); } + '</div><div class="col-md-6"><b>Age :</b>' + age + '</div><div class="col-md-12"><b>Mobile No : </b>' + mobile_no + '</div></div></body></html>';中没有错误或显示任何错误。

我该怎么办?

1 个答案:

答案 0 :(得分:1)

希望这会有所帮助,我使用serializeArray()来获取所有输入值

$("form").submit(function(e){
e.preventDefault()
var data = $('form').serializeArray()
var data1 = '<html><head><style media="print"></style></head><body><div class="row" style="border:2px solid #000;"><div class="col-md-12"><b>Name :</b>'+data[0]['value']+'</div><div class="col-md-6"><b>Gender :</b>'+data[5]['value']+'</div><div class="col-md-6"><b>Age :</b>'+data[1]['value']+'</div><div class="col-md-12"><b>Mobile No : </b>'+data[2]['value']+'</div></div></body></html>';
console.log(data1)

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<div class="form-group">
  <label for="form-first-name">Name</label>
  <input type="text" name="form-first-name" placeholder="name" class="form-first-name form-control require" id="name">
</div>
<div class="form-group">
  <label for="form-last-name">Age</label>
  <input type="number" name="form-last-name" placeholder="Age" class="form-last-name form-control require" id="age">
</div>
<div class="form-group">
  <label for="form-last-name">Mobile Number</label>
  <input type="text" name="form-last-name" placeholder="Mobile Number" class="form-last-name form-control require" id="mobile_number">
</div>
<div class="form-group">
  <label for="form-last-name">Religion</label>
  <input type="text" name="form-last-name" placeholder="Religion" class="form-last-name form-control require" id="religion">
</div>
<div class="form-group">
  <label for="form-last-name">Occupation</label>
  <input type="text" name="form-last-name" placeholder="Occupation" class="form-last-name form-control require" id="occupation" required>
</div>
<div class="form-group">
  <h4>Gender</h4>
  <div class="row">
    <div class="col-md-4">
      Male<input class="col-md-4" type="checkbox" name="gender" value="1">
    </div>
    <div class="col-md-4">
      Female<input class="col-md-4" type="checkbox" name="gender" value="2">
    </div>
    <div class="col-md-4">
      Other<input class="col-md-4" type="checkbox" name="gender" value="3">
    </div>
  </div>
</div>
<div class="form-group">
  <h4>Marital status</h4>
  <div class="row">
    <div class="col-md-4">
      Married<input type="checkbox" class="col-md-4" name="marital_status" value="1">
    </div>
    <div class="col-md-4">
      Single<input type="checkbox" name="marital_status" class="col-md-4" value="2">
    </div>
  </div>
  <input type="submit" value="submit">
 <form>