JavaScript禁用选择选项

时间:2015-02-12 14:33:42

标签: javascript select hidden

我有3个选择框,一个是另一个2的控件。 我的问题是,即使我从option_selector中选择option2,隐藏了option1框,表单仍然会尝试为option1 select传递一些东西。

从表格帖子刺痛的例子

www.blank.com/Main/NewCustomers/newappstodetails.asp?options=option2&varintBusinessTypeID=&varintBusinessTypeID=23

我应该只有一个varintBusinessTypeID。我需要select1被禁用而不是隐藏所以它不会尝试传递一些东西。

Example of below

  <select id="option_selector" name="options">
<option value="option1">Office Options</option>
<option value="option2">Retial Options</option>
</select>
<div id="options">
<div id="option1">
    <select name="varintBusinessTypeID" id="varintBusinessTypeID">
        <option value="">Please Select</option>
        <option value="1">Office1</option>
        <option value="2">Office2</option>
        <option value="3">Office3</option>
    </select>
</div>
<div id="option2">
    <select name="varintBusinessTypeID" id="varintBusinessTypeID">
        <option value="">Please Select</option>
        <option value="1">Retail1</option>
        <option value="2">Retail2</option>
        <option value="3">Retail3</option>
    </select>
</div>
</div>



$('#option_selector').change(refresh_inputs);
refresh_inputs();

function refresh_inputs() {
    var name = $('#option_selector').attr('name');
    var val = $('#option_selector').val();
    $('#' + name + ' div').hide();
    $('#' + val).show();
}

2 个答案:

答案 0 :(得分:2)

您可以将select的name属性设置为空字符串。

$(document).ready(function(){
    $('#option_selector').change(refresh_inputs);
    refresh_inputs();
});

function refresh_inputs() {
    var name = $('#option_selector').attr('name');
    var val = $('#option_selector').val();
    $('#' + name + ' div').hide();
    $('#' + name + ' div select').attr('name', '');
    $('#' + val).show();
    $('#' + val + ' select').attr('name', 'varintBusinessTypeID');
}

另外,您可能不希望DOM中的2个元素具有相同的ID(varintBusinessTypeID)。

答案 1 :(得分:0)

首先,ID必须是唯一的,您不能重复id="varintBusinessTypeID"

您可以使用类似

的内容
<select id="option_selector" name="options">
  <!-- ... -->
</select>
<select name="varintBusinessTypeID" id="varintBusinessTypeID-option1">
  <!-- ... -->
</select>
<select name="varintBusinessTypeID" id="varintBusinessTypeID-option2">
  <!-- ... -->
</select>

然后,要禁用select,您可以使用disabled属性(在jQuery中使用prop):

function refresh_inputs() {
    var val = $('#option_selector').val();
    $('[name="varintBusinessTypeID"]').hide().prop('disabled', true);
    $('#varintBusinessTypeID-'+val).show().prop('disabled', false);
}

$('#option_selector').change(refresh_inputs);
refresh_inputs();
function refresh_inputs() {
  var val = $('#option_selector').val();
  $('[name="varintBusinessTypeID"]').hide().prop('disabled', true);
  $('#varintBusinessTypeID-'+val).show().prop('disabled', false);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<form>
  <select id="option_selector" name="options">
    <option value="option1">Office Options</option>
    <option value="option2">Retial Options</option>
  </select>
  <div id="options">
    <select name="varintBusinessTypeID" id="varintBusinessTypeID-option1">
      <option value="">Please Select</option>
      <option value="1">Office1</option>
      <option value="2">Office2</option>
      <option value="3">Office3</option>
    </select>
    <select name="varintBusinessTypeID" id="varintBusinessTypeID-option2">
      <option value="">Please Select</option>
      <option value="1">Retail1</option>
      <option value="2">Retail2</option>
      <option value="3">Retail3</option>
    </select>
  </div>
  <input type="submit" />
</form>