简体   繁体   English

在jQuery中选中单选按钮后锁定选择元素的方法

[英]Way to lock a select element after a radio button is selected in jQuery

I have a select drop down filled with options and then I have two radios like so... 我有一个充满选项的选择下拉列表,然后我有两个收音机,就像这样...

<form>
  <select id="drop_down">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
  </select>

  <div id="radios">
    <input type="radio" value="blue" id="blue" name="radio_option">Blue</input>
    <input type="radio" value="green" id="green" name="radio_option">Green</input>
  </div>
</form>  

My goal is that once a user makes a selection in the drop down and then selects a radio button... then the select drop down will (for lack of a better term) "lock up", preventing the user from going back and changing their drop down selection. 我的目标是,一旦用户在下拉菜单中做出选择,然后选择单选按钮...则选择下拉菜单将(由于缺乏更好的用语)“锁定”,从而阻止用户返回并进行更改他们的下拉选择。

I know there's a way in jQuery to accomplish this... however I have not been able to find a way... 我知道jQuery中有一种方法可以实现这一目标...但是我却找不到方法...

This is my jQuery so far (it's wrong)... I am not sure what the event should be after the $('#drop_down') .... 到目前为止,这是我的jQuery(这是错误的)...我不确定$('#drop_down')之后的事件是什么。

$('#radios').change(function(){
  $('#drop_down').removeProp(select);
});

I realize this may be a simple solution... I'm just not finding the right event handler in the jQuery Docs... 我意识到这可能是一个简单的解决方案...我只是在jQuery Docs中找不到合适的事件处理程序...

Add the attribute disabled to the select element upon the radio input changing states. 在无线电输入更改状态时,将disabled的属性添加到select元素。 And remove the disabled attribut upon form submission, allowing the input to be sent. 并在提交表单时删除disabled属性,从而允许发送输入。

 $('#radios input').change(function(){ $('#drop_down').prop('disabled', true); }); $('form').on('submit', function() { $(this).find('#drop_down').removeAttr('disabled'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="" method="post"> <select id="drop_down"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> <option value="4">Four</option> </select> <div id="radios"> <input type="radio" value="blue" id="blue" name="radio_option">Blue</input> <input type="radio" value="green" id="blue" name="radio_option">Green</input> </div> <button type="submit">Submit</button> </form> 

You can set the button to be disabled, and update a hidden field to use the selected option, so that it also passes when you post the form. 您可以将按钮设置为禁用,并更新隐藏字段以使用选定的选项,以便在发布表单时也可以通过。

EDIT (adding the code) 编辑(添加代码)

The HTML (notice the last input still within the form): HTML(注意,最后输入仍在表单内):

<select id="drop_down">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
  <option value="4">Four</option>
</select>
<form id="radios">
  <input type="radio" value="blue" id="blue" name="radio_option" class="radios">Blue</input>
  <input type="radio" value="green" id="green" name="radio_option" class="radios">Green</input>
  <input type="hidden" id="myvalue" name="radio_option" />
</form>

The jQuery code (notice I'm giving a class to the radios above, and am using that in js, so that when I do the disabling, the hidden input doesn't get disabled): jQuery代码(请注意,我在上面的收音机中提供了一个类,并在js中使用它,因此当我执行禁用操作时,隐藏的输入不会被禁用):

$('.radios').change(function() {
  $(this).attr('disabled', true);
  $('#myvalue').val($(this).val());
}

Set the select's attribute: disabled="disabled" 设置选择的属性: disabled="disabled"

 alert($('select').val()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="drop_down" disabled="disabled"> <option value="1">One</option> <option value="2" selected>Two</option> <option value="3">Three</option> <option value="4">Four</option> <select> 

EDIT 编辑

As MarcB points out, if you need to get the value of the disabled select element, you can do so explicitly when the form is submitted. 正如MarcB所指出的,如果您需要获取disabled select元素的值,则可以在提交表单时显式地执行此操作。

There is an issue with your code, where you recycled the ID blue for both radio buttons. 您的代码存在问题,您将两个单选按钮的ID blue回收。 I am not sure if this is an intended feature or a typo, but you will need to rectify it as IDs are supposed to be unique in a document. 我不确定这是预期功能还是错字,但由于ID在文档中应该是唯一的,因此您需要对其进行更正。 In addition, <input /> is a self closing element, so the </input> tag is invalid. 另外, <input />是一个自闭元素,因此</input>标记无效。 Instead, use <label for="(id)"> . 而是使用<label for="(id)">

.removeProp() is likely not a good idea (so is .removeAttr() ) because attributes or properties that have been removed cannot be added back later. .removeProp()很可能不是一个好主意(所以是.removeAttr()因为已被删除属性或属性不能被添加回来。 Instead, use .prop('...', false) to "unset" a property. 而是使用.prop('...', false)来“取消设置”属性。 In your case, we want to disable the <select> element, and this is done by using .prop('disabled', true) . 在您的情况下,我们要禁用<select>元素,这可以通过使用.prop('disabled', true) However, since disabled elements will not have their values submitted , you will need to copy the final value of the select element into a hidden element, as per strategy mentioned in an answer to a related question . 但是,由于禁用的元素不会提交其值 ,因此您需要根据对相关问题的回答中提到的策略,将select元素的最终值复制到隐藏的元素中。

 $(function() { $('#radios').change(function(){ $('#drop_down') .prop('disabled', true) .next() .val($('#drop_down').val()); console.log($('#drop_down_final').val()); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="drop_down"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> <option value="4">Four</option> </select> <input type="hidden" id="drop_down_final" /> <form id="radios"> <input type="radio" value="blue" id="blue" name="radio_option" /><label for="blue">Blue</label> <input type="radio" value="green" id="green" name="radio_option" /><label for="green">Green</label> </form> 

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

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