如何使用submit for JavaScript捕获数据?
我需要改变背景和颜色的文本与2个不同的形式时,点击提交按钮的相同形式我有这个代码,已经可以改变文本如何使用submit for JavaScript捕获数据?,javascript,html,Javascript,Html,我需要改变背景和颜色的文本与2个不同的形式时,点击提交按钮的相同形式我有这个代码,已经可以改变文本 $('#选择')。更改(函数(){ if($(this).val()=='A'){ $(“h1”).css('background-color','white'); } if($(this).val()=='B'){ $(“h1”).css('background-color','red'); } if($(this).val()=='C'){ $(“h1”).css('background-c
$('#选择')。更改(函数(){
if($(this).val()=='A'){
$(“h1”).css('background-color','white');
}
if($(this).val()=='B'){
$(“h1”).css('background-color','red');
}
if($(this).val()=='C'){
$(“h1”).css('background-color','yellow');
}
如果($(this).val()=='D'){
$(“h1”).css('background-color','green');
}
});
$('#select1')。更改(函数(){
if($(this).val()=='A'){
$(“h1”).css('color','white');
}
if($(this).val()=='B'){
$(“h1”).css('color','red');
}
if($(this).val()=='C'){
$(“h1”).css('color','yellow');
}
如果($(this).val()=='D'){
$(“h1”).css('color','green');
}
});代码>
要更改的文本
背景文本
白色
红色
黄色的
绿色
彩色文本
白色
红色
黄色的
绿色
为简单起见,请使用ID设置表单:
<form id="form-id">
...
<input type="submit" value="submit">
</form>
VanillaJS:
document.getElementById('form-id').addEventListener('submit', function (e) {
e.preventDefault();
document.body.style.backgroundColor = 'red';
// some more JS
});
将其添加到函数中,并将ColorText
id添加到h3标记中;)
在按钮上添加ID:
<button id="submit">Change</button>
你到底想要什么?在提交时,您将被转移到另一个页面,您将看不到更改。单击提交,您将只看到所选颜色的更改
function capture(){
var e = document.getElementById("select");
var strUser = e.options[e.selectedIndex].text;
var e2 = document.getElementById("select1");
var strUser2 = e2.options[e2.selectedIndex].text;
document.body.style.backgroundColor=strUser;
document.getElementById("ColorText").style.color=strUser2;
}
<button id="submit">Change</button>
$("#submit").click(function(event){
// prevent the form from actually submitting
event.preventDefault();
var choices = {
"A": "white",
"B": "red",
"C": "yellow",
"D": "green"
};
var choiceBG = $("#select").val();
$("h1").css('background-color', choices[choiceBG]);
var choiceFG = $("#select1").val();
$("h1").css('color', choices[choiceFG]);
});