使输入文本字段可更改和可选择(通过复选框切换)

发布于 2024-12-27 13:09:27 字数 666 浏览 2 评论 0原文

请看我当前的 html 片段如下。我正在尝试使 html 输入字段可更改和可选择。因此,我可以更改输入文本字段中的颜色值,该字段通过相应的复选框进行切换。单击提交按钮时,仅提交所选颜色的信息。 我当前的html正确显示内容,但它会提交两种颜色信息。尽管只选中了一个复选框。

<form id="myForm">         
  <input type="checkbox" name="color1" value="blue_check" />  
  Color1: <input type="text" name="blue" value="120" /></br>  
  <input type="checkbox" name="color2" value="red_check" /> 
  Color2: <input type="text" name="red" value="160" /></br>   
  <input type="submit" value="OK" />    
</form>

或点击:http://jsfiddle.net/4xDFK/56/

提前谢谢您。

Please see my current html snippet is as below. I am trying to make html input fields changeable and selectable. So I can change the value of colors in input text field which is toggled with the corresponding checkbox. When the submit button is clicked, only the information of the selected color is submitted.
My current html displays the content correctly, but it will submit two colors information. Despite of only one checkbox is selected.

<form id="myForm">         
  <input type="checkbox" name="color1" value="blue_check" />  
  Color1: <input type="text" name="blue" value="120" /></br>  
  <input type="checkbox" name="color2" value="red_check" /> 
  Color2: <input type="text" name="red" value="160" /></br>   
  <input type="submit" value="OK" />    
</form>

or click on: http://jsfiddle.net/4xDFK/56/

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小矜持 2025-01-03 13:09:27

使用 jQuery:

$(document).ready(function() {
    $('#myForm :checkbox').each(function() {
        $(this).next("input").attr('disabled',!$(this).is(':checked'));
    });
});

$('#myForm :checkbox').change(function() {
    $(this).next("input").attr('disabled',!$(this).is(':checked'));
});

由于您没有提到 JavaScript 或 JS 框架的使用,因此建议使用纯 html 表单和服务器端评估来解决它:

  1. 为复选框指定一个清晰的名称,例如“check_blue”+“ check_red"
  2. 在服务器端,仅在选中相应的复选框时才处理颜色

如果您只想提交或允许根据复选框状态填充输入,则必须使用 JavaScript(并且可能应该在你的问题或者至少作为标签)。

With jQuery:

$(document).ready(function() {
    $('#myForm :checkbox').each(function() {
        $(this).next("input").attr('disabled',!$(this).is(':checked'));
    });
});

$('#myForm :checkbox').change(function() {
    $(this).next("input").attr('disabled',!$(this).is(':checked'));
});

As you don't mention the use of JavaScript or a JS-framework, here is a suggestion to solve it with the pure html form and server-side evaluation:

  1. Give the checkboxes a telling name, like "check_blue" + "check_red"
  2. On the server side, only process the color if the corresponding checkbox has been selected

If you only want to so submit or allow filling of the inputs according to the checkbox state, you have to use JavaScript (and should probably have mentioned that in your question or at least as a tag).

最丧也最甜 2025-01-03 13:09:27

如果未选中相关复选框,您可以在提交时删除元素。

如何阻止表单发送我们不想发送一些字段

you can remove elements on submit , if related checkbox is not checked .

how to prevent form from sending some fields we don't want to send

柠北森屋 2025-01-03 13:09:27

当您单击“提交”按钮时,所有输入值都会提交。所以你需要做的是在服务器端处理这个问题。为复选框命名并检查它们的值,谷歌搜索“处理 XXXX 中的复选框”,其中 XXXX 是您的服务器端语言。

<h1> Colors Information </h1>
<form id="myForm">
  <input type="checkbox" name="choice1" value="choice1"/>
  Color1: <input type="text" name="blue" value="120" /></br>
  <input type="checkbox" name="choice2" value="choice2"/>
  Color2: <input type="text" name="red" value="160" /></br>    
  <input type="submit" value="OK" />
</form>

然后,您可以检查选中了哪些复选框,并相应地查看是否应该考虑相应的文本输入字段。

All input values are submitted as you click hit the submit button. So what you need to do is to handle this on the server side. Give names to the checkboxes and check their values, google for "handling checkboxes in XXXX" where XXXX is your server side language.

<h1> Colors Information </h1>
<form id="myForm">
  <input type="checkbox" name="choice1" value="choice1"/>
  Color1: <input type="text" name="blue" value="120" /></br>
  <input type="checkbox" name="choice2" value="choice2"/>
  Color2: <input type="text" name="red" value="160" /></br>    
  <input type="submit" value="OK" />
</form>

You can then check which checkboxes are checked, and accordingly see if you should put the corresponding text input fields into consideration.

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