简体   繁体   English

HTML:在表单提交时隐藏输入单选

[英]HTML : hiding input radios on form submit

I've got a HTML form that has two possible types ("id" or "profile") 我有一个具有两种可能类型(“ id”或“ profile”)的HTML表单

<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 所以从本质上讲,我得到的URL是

/process.php?type=id&value=input

(or type=profile, depending on what the user picks) (或type = profile,取决于用户选择的内容)

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"? 1)以下jQuery / JavaScript代码是否“不好用”? 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? 2)有没有一种方法可以不使用htaccess规则或JavaScript?

Thanks! 谢谢!

As for your first question , I'd write my jQuery like this: 至于第一个问题 ,我会这样写我的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);
});

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. 如果您的js错误出现,则可以发送原始表单数据。 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. 您还可以减少js的使用,这将节省一些宝贵的带宽。

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). 将单选输入放在表单外,用id标识文本输入,以便您可以使用getElementById函数(如果浏览器支持,仍需要检查它)。 This avoids loading jQuery if you don't need it on this page. 如果您不需要此页面,则可以避免加载jQuery。

The result is the one expected by you. 结果是您期望的结果。

But.. I would use server-side processing of the form. 但是..我将使用服务器端处理表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM