嘿伙计们我在这里有一个jquery滑块的代码。我想取出滑块的值并使用http post方法将值提交给数据库。我有一个处理表单,我成功用于文本框和单选按钮。我希望将滑块的值保存在隐藏的字段中,然后发布它。我知道这从滑块获取值。
var selection = $( ".selector" ).slider( "value" );
现在我如何将此添加到此提交表单中的值中?
<input type='hidden' class="mainForm" id="fieldBox_5" name="field_5" value=selection>
这是我的全部代码。
<<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Slider - Slider bound to select</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="style.css">
<script>
$(function() {
var select = $( "#minbeds" );
var slider = $( "<div id='slider'></div>" ).insertAfter( select ).slider({
min: 1,
max: 10,
range: "min",
value: select[ 0 ].selectedIndex + 1,
slide: function( event, ui ) {
select[ 0 ].selectedIndex = ui.value - 1;
}
});
$( "#minbeds" ).change(function() {
slider.slider( "value", this.selectedIndex + 1 );
});
});
var selection = $( ".selector" ).slider( "value" );
</script>
</head>
<body>
<form id="mainForm_1" class="MainForm" method=post enctype=multipart/form-data action=processorss.php onSubmit="return validatePage1();">
<label for="minbeds" class="mainForm">Rate Your Prefence for Fish 1-10</label>
<select name="minbeds" id="minbeds">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>
<li class="mainForm">
<input type='hidden' class="mainForm" id="fieldBox_5" name="field_5" value=selection>
<input id="saveForm" class="mainForm" type="submit" value="Submit" />
</li>
</form>
</body>
</html>
答案 0 :(得分:1)
我添加了额外的代码来将所选值(从下拉列表或滑块)复制到隐藏字段。诀窍是,一旦获得所选项目的值,您需要自己将其复制到隐藏字段。
相关更改在此处(我也将其打印到控制台以验证值,您可以在检查它为您正常工作后删除这两行):
var selection = $("#slider").slider("value");
console.log("Slider value: " + selection);
$("#selectedNumber").val( selection );
console.log("Hidden field value: " + $("#selectedNumber").val());
请参阅以下演示:
$(function() {
var select = $("#minbeds");
var slider = $("<div id='slider'></div>").insertAfter(select).slider({
min: 1,
max: 10,
range: "min",
value: select[0].selectedIndex + 1,
slide: function(event, ui) {
select[0].selectedIndex = ui.value - 1;
$("#selectedNumber").val( ui.value - 1 );
console.log("Slider value: " + (ui.value - 1) );
console.log("Hidden field value: " + $("#selectedNumber").val());
}
});
$("#minbeds").change(function() {
slider.slider("value", this.selectedIndex + 1);
var selection = $("#slider").slider("value");
console.log("Slider value: " + selection);
$("#selectedNumber").val( selection );
console.log("Hidden field value: " + $("#selectedNumber").val());
});
});
#slider {
margin: 20px;
width: 400px;
}
<link href="https://code.jquery.com/ui/1.11.4/themes/black-tie/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<form id="mainForm_1" class="MainForm" method=post enctype=multipart/form-data action=processorss.php onSubmit="return validatePage1();">
<li class="mainForm">
<label for="minbeds" class="mainForm">Rate Your Prefence for Fish 1-10</label>
<select name="minbeds" id="minbeds">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>
<input type='hidden' class="mainForm" id="selectedNumber" name="selectedNumber" />
<input id="saveForm" class="mainForm" type="submit" value="Submit" />
</li>
</form>
答案 1 :(得分:0)
document.getElementById('fieldBox_5').value = selection;
或$('#fieldBox_5').val(selection);
(如果你更喜欢jQuery)啊,你需要在on ready方法中移动var selection
声明太