HTML:在表单提交上隐藏输入单选按钮

发布于 2024-12-20 14:06:32 字数 1158 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Saygoodbye 2024-12-27 14:06:33

您可以尝试这样的操作:

<input type="radio" name="type" value="id" onclick="getElementById('textValue').setAttribute('name','id');">
<input type="radio" name="type" value="profile" onclick="getElementById('textValue').setAttribute('name','profile');">

<form action="process.php">
<input type="text" id="textValue" name="value" value="input">
<input type="submit">
</form>

将无线电输入放在表单之外,让文本输入由 id 标识,以便您可以使用 getElementById 函数(仍需要检查浏览器是否支持) )。如果您在此页面上不需要 jQuery,这可以避免加载它。

结果就是你所期望的。

但是..我会使用表单的服务器端处理。

You could try something like this:

<input type="radio" name="type" value="id" onclick="getElementById('textValue').setAttribute('name','id');">
<input type="radio" name="type" value="profile" onclick="getElementById('textValue').setAttribute('name','profile');">

<form action="process.php">
<input type="text" id="textValue" name="value" value="input">
<input type="submit">
</form>

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.

葬﹪忆之殇 2024-12-27 14:06:32

至于你的第一个问题,我会像这样编写我的jQuery:

// store your form into a variable for reuse in the rest of the code
var form = $('form');

// bind a function to your form's submit.  this way you
// don't have to add an onclick attribute to your html
// in an attempt to separate your pre-submit logic from
// the content on the page
form.submit(function() {
        // text holds our text input
    var text = $('input:text', form),
        // radio holds all of our radio buttons
        radio = $('input:radio', form),
        // checked holds our radio button that is currently checked
        checked = $('input:radio:checked', form);

    // checking to see if checked exists (since the user can
    // skip checking radio buttons)
    if (checked) {
        // setting the name of our text input to our checked
        // radio button's value
        text.prop('name', checked.val());
    }

    // disabling our radio buttons (not sure why because the form
    // is about to submit which will take us to another page)
    radio.prop('disabled', true);
});

至于你的第二个问题,你总是可以将此逻辑移至服务器端。这取决于您希望预处理逻辑由客户端还是由服务器完成。无论哪种方式,您都应该在服务器上有逻辑来验证表单。如果您的 js 出错,它可能会发送原始表单数据。就我个人而言,我会将这个逻辑放在服务器上,以避免检查的开销,以确保它首先被预处理。您还可以减少 js 的使用,这将为您节省一些宝贵的带宽。

As for your first question, I'd write my jQuery like this:

// store your form into a variable for reuse in the rest of the code
var form = $('form');

// bind a function to your form's submit.  this way you
// don't have to add an onclick attribute to your html
// in an attempt to separate your pre-submit logic from
// the content on the page
form.submit(function() {
        // text holds our text input
    var text = $('input:text', form),
        // radio holds all of our radio buttons
        radio = $('input:radio', form),
        // checked holds our radio button that is currently checked
        checked = $('input:radio:checked', form);

    // checking to see if checked exists (since the user can
    // skip checking radio buttons)
    if (checked) {
        // setting the name of our text input to our checked
        // radio button's value
        text.prop('name', checked.val());
    }

    // disabling our radio buttons (not sure why because the form
    // is about to submit which will take us to another page)
    radio.prop('disabled', true);
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文