首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在javascript中创建函数以启用/禁用基于select的输入

在javascript中创建函数以启用/禁用基于select的输入
EN

Stack Overflow用户
提问于 2016-03-04 06:23:00
回答 3查看 82关注 0票数 1

我有基于选择的启用/禁用输入的代码,它可以工作。我意识到要使它成为一个函数,这样我就可以在需要的时候很容易地使用它。这是我目前的密码。

脚本

代码语言:javascript
复制
function ed(select, input){
    var $select= $(select),
        $input = $(input);
    $select.change(function() {
        if ($select.val() == 'Others') {
            $input.removeAttr('disabled');
        } else {
            $input.attr('disabled', 'disabled').val('');
        }
    }); //.trigger('change'); // added trigger to calculate initial state
        }

html

代码语言:javascript
复制
<form>
  <select id="trigger" change="ed('trigger', 'show-hide')" >
    <option>text here</option>
    <option>text2 here</option>
  </select>
  <input type="text" value="" id="show-hide" />
</form>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-03-04 06:25:43

id选择器应以#YOUR_ID作为前缀

每次调用change函数时,都会附加ed处理程序。

避免使用inline-event-binding

试试这个:

代码语言:javascript
复制
var $select = $('#trigger'),
  $input = $('#show-hide');
$select.change(function() {
  if ($select.val() == 'Others') {
    $input.removeAttr('disabled');
  } else {
    $input.attr('disabled', 'disabled').val('');
  }
}).trigger('change');
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form>
  <select id="trigger">
    <option>text here</option>
    <option>text2 here</option>
    <option value='Others'>Others</option>
  </select>
  <input type="text" value="" id="show-hide" />
</form>

使用您的方法的

代码语言:javascript
复制
function ed(select, input) {
  var $select = $('#' + select),
    $input = $('#' + input);
  if ($select.val() == 'Others') {
    $input.removeAttr('disabled');
  } else {
    $input.attr('disabled', 'disabled').val('');
  }
}
ed('trigger', 'show-hide');
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form>
  <select id="trigger" onchange="ed('trigger', 'show-hide')">
    <option>text here</option>
    <option>text2 here</option>
    <option value='Others'>Others</option>
  </select>
  <input type="text" value="" id="show-hide" />
</form>

注意: onchange应该是属性,用于侦听更改事件,而不是change

编辑:在您当前的代码中,实现了inlinejavascript事件绑定,这将导致意外的输出。

票数 3
EN

Stack Overflow用户

发布于 2016-03-04 06:27:25

替换

代码语言:javascript
复制
 change="ed('trigger', 'show-hide')"

使用

代码语言:javascript
复制
 change="ed('#trigger', '#show-hide')"

正如人造丝的回答上面提到的,您需要传递id选择器。

这是整体工作代码

代码语言:javascript
复制
<select name="state" class="select" onchange="ed('#state', '#province')" id="state">
    <option value="something">Something</option>
    <option value="Others">Other</option>
</select>

<input type="text" name="province" class="text" id="province" />

请注意,上面的change已被onchange替换

代码语言:javascript
复制
function ed(select, input)
{
    var $select = $(select),
    $input = $(input);
    if ($select.val() == 'Others') {
        $input.removeAttr('disabled');
    } else {
        $input.attr('disabled', 'disabled').val('');
    }
}
$(document).ready( function(){

   ed('#state', '#province');

} );
票数 1
EN

Stack Overflow用户

发布于 2016-03-04 06:38:41

脚本:

代码语言:javascript
复制
function enableDisable(selectName, inputName){
     var selectVal = $("select[name='"+selectName+"']").val();
     if(selectVal == 'Others'){ 
          $("input[name='"+inputName+"']").removeAttr('disabled');
     }else{
          $("input[name='"+inputName+"']").attr('disabled','disabled').val('');
     } 
    }

HTML :

代码语言:javascript
复制
<form>
  <select name="trigger" change="enableDisable('trigger', 'show-hide')" >
    <option value = "1">text here</option>
    <option value = "Others">Others</option>
  </select>
  <input type="text" value="" name="show-hide" />
</form>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35789325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档