Javascript 在选择菜单JQuery中选择选项时添加
我有一个表格,里面有各种各样的数据,但我想做一件事,我有一个国家下拉列表,名为“土地”。我希望这样,如果我选择荷兰,行中有一个名为provincie的新列。但如果我选择了一个国家,而不是一个国家,provincie不应该出现。我会尽可能地提供给你 通过此JSFIDLE: 我还将把代码放在这里,因为stackoverflow推荐了它 演示 $document.readyfunction{ $'input[name=submit]'。单击函数E{ e、 防止违约; formIsValid=true; var错误=[]; $'.errors'.html; $'input.required'。每个函数{ 如果$this.val=={ formIsValid=false; message=$this.attr'id'+'是verplicht'; 错误。推送消息; $this.addClassred; }否则{ $this.removeClassred; } }; 如果formIsValid==true{ $'.data'.append++$'input[name=name]'.val++$'input[name=email]'.val++$'input[name=phone_number]'.val++$'land选项:选中'.text++'X'; updateTotalRows; $'.delete'。单击函数{ $this.parent.remove; updateTotalRows; } } }; 函数updateTotalRows{ $'.total'.html'Ik heb nu:'+$'.datarow'.length+'rows'; } }; 荷兰 杜伊特兰 弗兰克瑞克 纳姆 电子邮件 telefoonnummer 土地 css: 瑞德先生{ 边框颜色:红色; } .下拉菜单{ 背景色:FFF; 列表样式:无; }Javascript 在选择菜单JQuery中选择选项时添加,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个表格,里面有各种各样的数据,但我想做一件事,我有一个国家下拉列表,名为“土地”。我希望这样,如果我选择荷兰,行中有一个名为provincie的新列。但如果我选择了一个国家,而不是一个国家,provincie不应该出现。我会尽可能地提供给你 通过此JSFIDLE: 我还将把代码放在这里,因为stackoverflow推荐了它 演示 $document.readyfunction{ $'input[name=submit]'。单击函数E{ e、 防止违约; formIsValid=true;
看看这两把小提琴。第一个用于从下拉列表中获取值。就你而言,荷兰 第二种方法是动态地将列添加到表中
<table border="1" id="mtable">
<thead><tr><td>Item</td><td>Red</td><td>Green</td></tr></thead>
<tbody><tr><td>Some Item</td><td><input type="checkbox"/></td><td><input type="checkbox"/></td></tr></tbody>
插入行
插入列
还有Js
$('#irow').click(function(){
if($('#row').val()){
$('#mtable tbody').append($("#mtable tbody tr:last").clone());
$('#mtable tbody tr:last :checkbox').attr('checked',false);
$('#mtable tbody tr:last td:first').html($('#row').val());
}else{alert('Enter Text');}
});
$('#icol').click(function(){
if($('#col').val()){
$('#mtable tr').append($("<td>"));
$('#mtable thead tr>td:last').html($('#col').val());
$('#mtable tbody tr').each(function() {$(this).children('td:last').append($('<input type="checkbox">'))});
}else{alert('Enter Text');}
});
要删除不应该显示的列/行,只需给样式不可见的div指定一个类,并在需要显示/删除div时切换该div,解决方案如下:
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="nadal.css">
<meta charset="utf-8">
<title>Demo</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
$(document).ready(function(){
$('input[name="submit"]').click(function(e) {
e.preventDefault();
formIsValid = true;
var errors = [];
$('.errors').html("");
$('input.required').each(function() {
if ($(this).val() == '') {
formIsValid = false;
message = $(this).attr('id') + ' is verplicht';
errors.push(message);
$(this).addClass("red");
} else{
$(this).removeClass("red");
}
});
if (formIsValid == true) {
var provincie ="";
if ($('.province' ).val().trim() !="") {
provincie = $('.province' ).val();
}
$('.data').append('<tr class="datarow"><td>'+$('input[name="name"]').val()+'</td><td>'+$('input[name="email"]').val()+'</td><td>'+$('input[name="phone_number"]').val()+'</td><td>'+$('#land option:selected').text()+ ' ' + provincie +'</td><td class="delete">X</td></tr>');
updateTotalRows();
$('.delete').click(function() {
$(this).parent().remove();
updateTotalRows();
})
}
});
function updateTotalRows() {
$('.total').html('Ik heb nu : ' + $('.datarow').length + ' rows');
}
$('#land').on('change', function(){
if($("#land").val()=="Nederland"){
$('.province').show();
$('#table thead tr th:contains(Province)').show();
}
else{
$('.province').hide();
$('#table thead tr th:contains(Province)').hide();
}
});
});
</script>
<form id="myForm">
<div class="errors"></div>
<input type="text" id="Naam" class="required" name="name" placeholder="name" >
<input type="email" id="Email" name="email" class="required" placeholder="email">
<input type="number" id="Telefoonnummer" name="phone_number" class="required" placeholder="phone">
<select name="land" id="land">
<option value="Nederland">Nederland</option>
<option value="Duitsland">Duitsland</option>
<option value="Frankrijk">Frankrijk</option>
</select>
<input type="text" class="province" placeholder="enter province">
<input type="submit" name="submit">
</form>
<form id="myFormCorrect">
<table id="table">
<thead>
<tr>
<th>Naam</th>
<th>Email</th>
<th>telefoonnummer</th>
<th>Land</th>
<th>Province</th>
<th> </th>
</tr>
</thead>
<tbody class="data">
</tbody>
</table>
</form>
<div class="total"></div>
</body>
</html>
对不起,我听不懂你的问题。。。请检查这是您想要的吗?更新是我想要的,但是当您提交时,provincie不会出现在表中。@NadalRahman检查一下
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="nadal.css">
<meta charset="utf-8">
<title>Demo</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
$(document).ready(function(){
$('input[name="submit"]').click(function(e) {
e.preventDefault();
formIsValid = true;
var errors = [];
$('.errors').html("");
$('input.required').each(function() {
if ($(this).val() == '') {
formIsValid = false;
message = $(this).attr('id') + ' is verplicht';
errors.push(message);
$(this).addClass("red");
} else{
$(this).removeClass("red");
}
});
if (formIsValid == true) {
var provincie ="";
if ($('.province' ).val().trim() !="") {
provincie = $('.province' ).val();
}
$('.data').append('<tr class="datarow"><td>'+$('input[name="name"]').val()+'</td><td>'+$('input[name="email"]').val()+'</td><td>'+$('input[name="phone_number"]').val()+'</td><td>'+$('#land option:selected').text()+ ' ' + provincie +'</td><td class="delete">X</td></tr>');
updateTotalRows();
$('.delete').click(function() {
$(this).parent().remove();
updateTotalRows();
})
}
});
function updateTotalRows() {
$('.total').html('Ik heb nu : ' + $('.datarow').length + ' rows');
}
$('#land').on('change', function(){
if($("#land").val()=="Nederland"){
$('.province').show();
$('#table thead tr th:contains(Province)').show();
}
else{
$('.province').hide();
$('#table thead tr th:contains(Province)').hide();
}
});
});
</script>
<form id="myForm">
<div class="errors"></div>
<input type="text" id="Naam" class="required" name="name" placeholder="name" >
<input type="email" id="Email" name="email" class="required" placeholder="email">
<input type="number" id="Telefoonnummer" name="phone_number" class="required" placeholder="phone">
<select name="land" id="land">
<option value="Nederland">Nederland</option>
<option value="Duitsland">Duitsland</option>
<option value="Frankrijk">Frankrijk</option>
</select>
<input type="text" class="province" placeholder="enter province">
<input type="submit" name="submit">
</form>
<form id="myFormCorrect">
<table id="table">
<thead>
<tr>
<th>Naam</th>
<th>Email</th>
<th>telefoonnummer</th>
<th>Land</th>
<th>Province</th>
<th> </th>
</tr>
</thead>
<tbody class="data">
</tbody>
</table>
</form>
<div class="total"></div>
</body>
</html>