HTML:在表单提交上隐藏输入单选按钮
我有一个 HTML 表单,有两种可能的类型(“id”或“个人资料”),
<form action="process.php">
<input type="radio" name="type" value="id">
<input type="radio" name="type" value="profile">
<input type="text" name="value" value="input">
</form>
所以本质上,我生成的 URL 是
/process.php?type=id&value=input
(或 type=profile,取决于用户选择的内容)
我想要的是我的 URL 看起来像什么喜欢
/process.php?id=input
或者
/process.php?profile=input
我有两个问题:
1)以下 jQuery/JavaScript 代码是“不好的做法”吗?如果我按照以下方式执行此操作(即使它有效),我真的不觉得自己做得正确:
<input type="submit" onclick="formSubmit()">
<script>
function formSubmit() {
// if the first radio (id) is selected, set value name to "id", else "profile"
$("form input:text")[0].name = $("form input:radio")[0].checked ? "id" : "profile";
// disable radio buttons (i.e. do not submit)
$("form input:radio")[0].disabled = true;
$("form input:radio")[1].disabled = true;
}
</script>
2)有没有一种方法可以在不使用 htaccess 规则或 JavaScript 的情况下执行此操作?
谢谢!
I've got a HTML form that has two possible types ("id" or "profile")
<form action="process.php">
<input type="radio" name="type" value="id">
<input type="radio" name="type" value="profile">
<input type="text" name="value" value="input">
</form>
So essentially, my resulting URL is
/process.php?type=id&value=input
(or type=profile, depending on what the user picks)
What I want is my URLs to look something like
/process.php?id=input
or
/process.php?profile=input
I have two questions:
1) Is the following jQuery/JavaScript code "bad practice"? I really don't feel like I'm doing it correctly if I do it the following way (even though it works):
<input type="submit" onclick="formSubmit()">
<script>
function formSubmit() {
// if the first radio (id) is selected, set value name to "id", else "profile"
$("form input:text")[0].name = $("form input:radio")[0].checked ? "id" : "profile";
// disable radio buttons (i.e. do not submit)
$("form input:radio")[0].disabled = true;
$("form input:radio")[1].disabled = true;
}
</script>
2) Is there a way to do this without using htaccess rules or JavaScript?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试这样的操作:
将无线电输入放在表单之外,让文本输入由 id 标识,以便您可以使用
getElementById
函数(仍需要检查浏览器是否支持) )。如果您在此页面上不需要 jQuery,这可以避免加载它。结果就是你所期望的。
但是..我会使用表单的服务器端处理。
You could try something like this:
Put the radio inputs outside the form, have the text input identified by an id so you can use the
getElementById
function (still needs to be checked if it is supported by the browser). This avoids loading jQuery if you don't need it on this page.The result is the one expected by you.
But.. I would use server-side processing of the form.
至于你的第一个问题,我会像这样编写我的jQuery:
至于你的第二个问题,你总是可以将此逻辑移至服务器端。这取决于您希望预处理逻辑由客户端还是由服务器完成。无论哪种方式,您都应该在服务器上有逻辑来验证表单。如果您的 js 出错,它可能会发送原始表单数据。就我个人而言,我会将这个逻辑放在服务器上,以避免检查的开销,以确保它首先被预处理。您还可以减少 js 的使用,这将为您节省一些宝贵的带宽。
As for your first question, I'd write my jQuery like this:
As for your second question, you could always move this logic to the server side. It depends if you want the pre-processing logic to be done by the client, or by your server. Either way you should have logic on the server to validate the form. If your js errors out, it could send over the raw form data. Personally I'd put this logic on the server to avoid the overhead of checking to make sure it was pre-processed in the first place. You'll also be able to cut down on your js use which will save you some precious bandwidth.