javascript 单选按钮值在表单提交时发送为“on”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19577484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
radio button value being sent as "on" upon form submission
提问by Jawwad Zakaria
I have a form with three radio button options. I need to submit the form data to another file, but for some reason the data sent contains the value of the selected radio button as "on" as opposed to the value of the value attribute.
我有一个包含三个单选按钮选项的表单。我需要将表单数据提交到另一个文件,但由于某种原因,发送的数据包含所选单选按钮的值为“on”,而不是 value 属性的值。
I tried manually manipulating and sending the data via the post() function but since I need to redirect the page upon submission, that results in a POST request followed by a GET request. The actual purpose of this code requires me to send this data to a function which handles the data and decides where to redirect the page. However, that function has different uses for GET requests and gives errors when this form sends a GET request. That limitation is what is making this a problem in the first place.
我尝试通过 post() 函数手动操作和发送数据,但由于我需要在提交时重定向页面,这导致 POST 请求后跟 GET 请求。这段代码的实际用途要求我将这些数据发送到一个处理数据并决定将页面重定向到何处的函数。但是,该函数对 GET 请求有不同的用途,并且在此表单发送 GET 请求时会出错。这种限制是首先使这成为问题的原因。
Is there a way to somehow get the value of the radio button to be posted instead of just "on" such that there is only one request sent from this form? I understand if the only way is to modify the function that the data is sent to.
有没有办法以某种方式获取要发布的单选按钮的值,而不仅仅是“打开”,以便仅从该表单发送一个请求?我了解是否唯一的方法是修改数据发送到的函数。
Please comment if any of this is unclear or confusing. Thank you for your help!
如果其中任何一个不清楚或令人困惑,请发表评论。谢谢您的帮助!
Code:
代码:
<form name="send-form" class="send-form" method="POST" action="somefunction>
<input type="radio" name="option" id="1" val="1">
<input type="radio" name="option" id="2" val="2">
<input type="radio" name="option" id="3" val="3">
</form>
<script>
$(".send-form").submit(function(e){
e.preventDefault();
// get selected value
var selected = $("input[type='radio'][name='option']:checked");
// check if an option was selected
if (selected.length > 0) {
selectedValue = selected.attr("id");
$.post('somefile', {"option" : selectedValue}, function() {
window.location.href = "somefile";
});
}
} else {
alert("Please select an option");
}
});
</script>
回答by Mike Christensen
You need to use value
, instead of val
:
您需要使用value
, 而不是val
:
<form name="send-form" class="send-form" method="POST" action="somefunction">
<input type="radio" name="option" id="1" value="1" />
<input type="radio" name="option" id="2" value="2" />
<input type="radio" name="option" id="3" value="3" />
</form>
This will post option=1
, option=2
, or option=3
in the request.
这将在请求中发布option=1
、option=2
或option=3
。
回答by Paul
On first pass I noticed you don't have a closing " on your form tag.
第一次通过时,我注意到您的表单标签上没有结束“。