Javascript 如何绑定引导按钮复选框和窗体?
有一个代码如下所示:Javascript 如何绑定引导按钮复选框和窗体?,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,有一个代码如下所示: <form action="/test.html" method="get"> <p><b>Question?</b></p> <p><input type="radio" name="answer" value="a">C<Br> <input type="radio" name="answer" value="b">B<Br&g
<form action="/test.html" method="get">
<p><b>Question?</b></p>
<p><input type="radio" name="answer" value="a">C<Br>
<input type="radio" name="answer" value="b">B<Br>
<input type="radio" name="answer" value="c">C</p>
<div class="btn-group" data-toggle="buttons-checkbox">
<button type="button" name="bb" value="b1" class="btn btn-primary">Left</button>
<button type="button" name="bb" value="b2" class="btn btn-primary">Middle</button>
<button type="button" name="bb" value="b3" class="btn btn-primary">Right</button>
</div>
<p><input type="submit"></p>
</form>
问题
C
B
C
左边
中间的
赖特
如何将其发送到get方法?可能需要编写一些js(jquery)代码,但我想不出任何东西。您在
下的按钮。btn group
没有提交,因为它们的类型不正确。您需要将type=“button”
更改为type=“submit”
。只有具有type=“submit”
的按钮才会提交父级表单(默认按钮类型为submit
)
左边
中间的
赖特
您遇到的问题是按钮值未提交
以下内容将为提交表单时选择的按钮创建隐藏的输入元素
<form action="/test.html" id="the-form" method="get">
<p><b>Question?</b></p>
<p><input type="radio" name="answer" value="a">C<Br>
<input type="radio" name="answer" value="b">B<Br>
<input type="radio" name="answer" value="c">C</p>
<div class="btn-group" data-toggle="buttons-checkbox">
<button type="button" name="bb1" value="b1" class="btn btn-primary">Left</button>
<button type="button" name="bb2" value="b2" class="btn btn-primary">Middle</button>
<button type="button" name="bb3" value="b3" class="btn btn-primary">Right</button>
</div>
<p><input type="submit"></p>
</form>
<script>
$('#the-form').on('submit', function() {
$('.btn.active', '#the-form').each(function() {
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", this.name);
input.setAttribute("value", this.value);
document.getElementById("the-form").appendChild(input);
});
});
</script>
您的代码已提交到/test.html
页面:
sushain97,但来自按钮复选框的数据不发送,关于此问题。Dipesh Parmar我尝试了$.get(),但由于某些原因它不起作用。此外,还必须为标准输入字段编写代码。
<form action="/test.html" id="the-form" method="get">
<p><b>Question?</b></p>
<p><input type="radio" name="answer" value="a">C<Br>
<input type="radio" name="answer" value="b">B<Br>
<input type="radio" name="answer" value="c">C</p>
<div class="btn-group" data-toggle="buttons-checkbox">
<button type="button" name="bb1" value="b1" class="btn btn-primary">Left</button>
<button type="button" name="bb2" value="b2" class="btn btn-primary">Middle</button>
<button type="button" name="bb3" value="b3" class="btn btn-primary">Right</button>
</div>
<p><input type="submit"></p>
</form>
<script>
$('#the-form').on('submit', function() {
$('.btn.active', '#the-form').each(function() {
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", this.name);
input.setAttribute("value", this.value);
document.getElementById("the-form").appendChild(input);
});
});
</script>
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" name="bb" value="b1" class="btn btn-primary">Left</button>
<button type="button" name="bb" value="b2" class="btn btn-primary">Middle</button>
<button type="button" name="bb" value="b3" class="btn btn-primary">Right</button>
</div>